使用“使用”感到困惑语句 C#

发布于 2024-09-29 06:09:27 字数 1650 浏览 0 评论 0原文

根据MSDN Library

using 语句(C# 参考)
定义一个范围,在该范围之外将处理一个或多个对象。

但是我得到了一些用户在此处发布的代码,我对此感到困惑:(请参阅我对代码的评论)

using (OleDBConnection connection = new OleDBConnection(connectiongString))
           {
                if (connection.State != ConnectionState.Open)
                    connection.Open();
                string sql = "INSERT INTO Student (Id, Name) VALUES (@idParameter, @nameParameter)";

                using (OleDBCommand command = connection.CreateCommand())
                {
                    command.CommandText = sql;
                    command.CommandType = CommandType.Text;

                    OleDBParameter idParameter = command.CreateParameter();
                    idParameter.DbType = System.Int32;
                    idParameter.Direction = Parameterdirection.Input;
                    idParameter.Name = "@idParameter";
                    idParameter.Value = studentId; 

                    OleDBParameter nameParameter = command.CreateParameter();
                    try
                    {
                        command.ExecuteNonQuery();
                    }
                    finally
                    {
                        // Is it still necessary to dispose these objects here?
                        command.Dispose();
                        connection.Dispose();
                    }
                }
            }

在上面的代码中, using 语句是否正确使用? 我很困惑,谁能解释一下如何使用 using 语句及其范围以及何时、何地以及为何使用它。谢谢..

According to MSDN Library

using Statement (C# Reference)
Defines a scope, outside of which an object or objects will be disposed.

But I got this code posted here by some user and I got confused about this: (please see my comment on the code)

using (OleDBConnection connection = new OleDBConnection(connectiongString))
           {
                if (connection.State != ConnectionState.Open)
                    connection.Open();
                string sql = "INSERT INTO Student (Id, Name) VALUES (@idParameter, @nameParameter)";

                using (OleDBCommand command = connection.CreateCommand())
                {
                    command.CommandText = sql;
                    command.CommandType = CommandType.Text;

                    OleDBParameter idParameter = command.CreateParameter();
                    idParameter.DbType = System.Int32;
                    idParameter.Direction = Parameterdirection.Input;
                    idParameter.Name = "@idParameter";
                    idParameter.Value = studentId; 

                    OleDBParameter nameParameter = command.CreateParameter();
                    try
                    {
                        command.ExecuteNonQuery();
                    }
                    finally
                    {
                        // Is it still necessary to dispose these objects here?
                        command.Dispose();
                        connection.Dispose();
                    }
                }
            }

In the above code, does the using statement properly used?
I'm confused, can anyone please explain how to use using statement and its scoping and when, where and why to use it. Thank you..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰つ倒转 2024-10-06 06:09:27

using 语句是手动放置 try/finally 块的简写。

所以

using( x ){
  ...
}

是一样的

try{
  ...
}finally{
  if( x != null ){ x.Dispose(); }
}

并且它们在编译时会生成相同的 IL。

如果 x 未实现 IDisposable,编译器将给出错误。

The using statement is a shorthand for manually putting a try/finally block.

So

using( x ){
  ...
}

Is the same as

try{
  ...
}finally{
  if( x != null ){ x.Dispose(); }
}

And they will generate the same IL when compiled.

The compiler will give you an error in case x does not implement IDisposable.

半葬歌 2024-10-06 06:09:27

finally 块(因此在本例中为 try)是多余的,这就是 using 的作用,它调用 Dispose > 在 IDisposable 对象上,当 using 块结束时使用该对象进行初始化(无论是否存在异常)。

The finally block (and thus in this case the try) is redundant, that's what using does, it calls Dispose on the IDisposable object with which it is initialized when the using block ends (regardless of exceptions or lack thereof).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文