Reputation:
In the following code, are the using blocks redundant or are they necessay to fully release resources?
using (var dialog = new AboutBox())
dialog.ShowDialog();
using (var form = new OptionForm())
form.Show();
Upvotes: 5
Views: 107
Reputation: 755317
The first example is not redundant. You should always dispose of an IDisposable
the moment you are done with it and in the case of a modal form this exactly accomplishes the goal.
The second example though will lead to errors. The Show
method returns immediately and the form continues to be displayed. However the generated using
code will immediately Dispose
the form and cause it to go away. The form should only be disposed once it's finished showing.
Upvotes: 6