Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use a using statement to create a Windows.Forms.Form object?

I've read (on using Statement (C# Reference)) that the using statement should be used to release the resources used by managed types (like File and Font) that use unmanaged resources. So started using it with MySql classes and related stuff, but if you take a look at an object of a Windows.Forms.Form class you will see a Dispose method, it means that this class implements IDisposable so, should I use a using statement for a Windows.Forms.Form object like in the case below?

private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
    using (AboutBoxProjeto about = new AboutBoxProjeto())
    {
        about.ShowDialog();
    }
}
like image 407
Zignd Avatar asked Jan 14 '23 08:01

Zignd


1 Answers

From http://dotnetfacts.blogspot.com/2008/03/things-you-must-dispose.html:

In .NET, a dialog form is a form opened by calling the ShowDialog() method. Unlike modeless forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is not closed, you must call the Dispose() method of the form when the form is no longer needed by your application

like image 146
Giedrius Avatar answered Jan 31 '23 09:01

Giedrius