When should I use the using Statement? [duplicate]

2020-01-27 04:51发布

Possible Duplicate:
What is the C# Using block and why should I use it?

I'm converting an old site to C# and I'm not sure when I should be using 'using'. Are there any general guidelines? I know of the benefits, but I'm not 100% sure how to use it. Is it every time I 'new' a method/property?

SqlConnection awesomeConn = new SqlConnection(connection);

5条回答
我欲成王,谁敢阻挡
2楼-- · 2020-01-27 05:20

If a class implements IDisposable then it will create some unmanaged resources which need to be 'disposed' of when you are finished using them. So you would do something like:

SqlConnection awesomeConn = new SqlConnection(connection);

// Do some stuff

awesomeConn.Dispose();

To avoid forgetting to dispose of the resourses (in this case close the database connection), especially when an exception is thrown, you can use the using syntax to automatically call dispose when you go out of the using statement's scope:

using (SqlConnection awesomeConn = new SqlConnection(connection))
{
     // Do some stuff
} // automatically does the .Dispose call as if it was in a finally block

In fact, the using block is equivalent to:

try
{
    SqlConnection awesomeConn = new SqlConnection(connection);

    // do some stuff
}
finally 
{
    awesomeConn.Dispose();
}
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-01-27 05:29

Using is a convenience that allows you to assure that you can't exit a block with out disposing of the resource. It can and should be utilized whenever you have to utilize an IDisposable implementer in a local code block.

查看更多
Rolldiameter
4楼-- · 2020-01-27 05:31

Use using for all objects which you instantiate that implement IDisposable unless their lifetime extends beyond the current scope of execution (I.e. method call). In that case, for instance if you have a disposable member variable, then the containing class should implement IDisposable and Dispose members in its Dispose.

查看更多
Deceive 欺骗
5楼-- · 2020-01-27 05:32

It is often used if you want to automatically dispose objects. Otherwise you have to call myobj.Dispose() manually.

See the reference documentiation here: http://msdn.microsoft.com/en-us/library/yh598w02.aspx

查看更多
6楼-- · 2020-01-27 05:42

MSDN:

As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object):

Example:

using (StreamReader stream = new StreamReader("path")) 
{
     string line = stream.ReadLine();
}
查看更多
登录 后发表回答