Thursday, July 23, 2015

Use of Using keyword

Use of Using keyword
The reason for the "using" statement is to ensure that the object is disposed as soon as it goes out of scope, and it doesn't require explicit code to ensure that this happens.
The .NET CLR converts
using (MyResource myRes = new MyResource()) { myRes.DoSomething(); }
to
// 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(); } }

No comments:

Post a Comment