Wednesday, August 12, 2009

Using statement .NET

Whoever working in C# come across using Directive using System ;
What about using Statement ? what it does is...

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.







{
Font font1 = new Font("Arial", 10.0f);
try
{
byte charset = font1.GdiCharSet;
}
finally
{
if (font1 != null)
((IDisposable)font1).Dispose();
}
}
Solution:You can use like this

using (Font font1 = new Font("Arial", 10.0f))
{
byte charset = font1.GdiCharSet;
}

Thanks:MSDN
http://msdn.microsoft.com/en-us/library/yh598w02.aspx

No comments:

Post a Comment