So a using statement automatically calls the dispose method on the object that is being "used", when the using block is exited, right?
But when is this necessary/beneficial?
For example let's say you have this method:
public void DoSomething()
{
using (Font font1 = new Font("Arial", 10.0f))
{
// Draw some text here
}
}
Is it necessary to have the using statement here, since the object is created in the method? When the method exits, wont the Font object be disposed of anyway?
Or does the Dispose method get run at another time after the method exits?
For example if the method was like this:
public void DoSomething()
{
Font font1 = new Font("Arial", 10.0f);
// Draw some text here
}
// Is everything disposed or cleared after the method has finished running?