This (the using statment) is an important point. If you use a class that
needs disposing, and it is not disposed, it can cause some serious problems,
depending on the class being used. A file that is opened, for example, will
cause the file to remain locked, until the app exits. So, it is important to
ensure that Disposable objects are Disposed. This *can* be done using
try/catch/finally, as in:
try
{
Pen bluePen = new Pen( Color.Blue, 2 );
//use pen
}
finally
{
if (bluePen != null) bluePen.Dispose();
}
However, if you use a using statement, it is much simpler:
using (Pen bluePen = new Pen(Color.Blue, 2)
{
//use pen
}
The using statement automatically disposes the object created when it passes
out of scope.
--
HTH,
Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist
What You Seek Is What You Get.
"Roger" <ro***@pcsrevenuecontrol.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Also, see Using statement (the one about freeing
resources).
Roger
>I am new to c# and have become confused about freeing resources. I need
to
>know if using an objects Dispose method does the same thing as setting
the
object to null?
Pen bluePen = new Pen( Color.Blue, 2 );
//use pen
bluePen.Dispose();
as opposed to:
Pen bluePen = new Pen(Color.Blue, 2);
//use pen
bluePen = null;
I appreciate your help
thank you
bill