The reason for the "using" statement is to ensure that the object is always disposed correctly, and it doesn't require explicit code to ensure that this happens.
{ // limits scope of myRes
MyResource myRes= new MyResource();
try
{
myRes.DoSomething();
}
finally
{
// Check for a null resource.
if (myRes!= null)
// Call the object's Dispose method.
((IDisposable)myRes).Dispose();
}
}
We can write above code like this
using (MyResource myRes = new MyResource())
{
myRes.DoSomething();
}
Liked By
Write Answer
The "using" Keyword in C#
Join MindStick Community
You have need login or register for voting of answers or question.
Shankar M
09-Feb-2013AVADHESH PATEL
19-Nov-2012hi Pravesh
The reason for the "using" statement is to ensure that the object is always disposed correctly, and it doesn't require explicit code to ensure that this happens.
We can write above code like this