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);
The using statement is used to set one or more than one resource. These resources are executed and the resource is released. The statement is also used with database operations. The main goal is to manage resources and release all the resources automatically.
The "using" statement allows you to specify multiple resources in a single statement. The object could also be created outside the "using" statement. The objects specified within the using block must implement the IDisposable interface.
The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.
C# provides a special "using" statement to call Dispose method explicitly. using statement gives you a proper way to call the Dispose method on the object. In using statement, we instantiate an object in the statement. At the end of using statement block, it automatically calls the Dispose method.
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();
}
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With