When should I use the using Statement? [duplicate]

旧街凉风 提交于 2019-11-26 12:45:01

问题


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);

回答1:


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();
}



回答2:


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();
}



回答3:


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.




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/10057334/when-should-i-use-the-using-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!