I have this example for a method that responds to the paint event. My question is regarding the using of the using statement. What is so special about a font that it is put in the using statement? I understand if it was a stream or something else where it would be automatically closed. But why use it here? Is it so the font will be disposed?
private void Form1_Paint(object sender, PaintEventArgs e)
{
using (Font font = new Font(new FontFamily("Arial"), 28, FontStyle.Regular, GraphicsUnit.Pixel))
{
Point point1 = new Point(10,10);
TextRenderer.DrawText(e.Graphics, "Banner Text", font, point1, Color.Blue);
}
}
It is used this way to ensure that the Font is properly disposed of.
From using Statement (C# Reference)
File and Font are examples of managed types that access unmanaged resources (in this case file handles and device contexts). There are many other kinds of unmanaged resources and class library types that encapsulate them. All such types must implement the IDisposable interface.