Is it too difficult to wrap dialogs when using blocks?

Does the following code use redundant usage blocks or are they needed to completely free resources?

using (var dialog = new AboutBox())
    dialog.ShowDialog();

using (var form = new OptionForm())
    form.Show();
+5
source share
1 answer

The first example is not redundant. You should always dispose IDisposableof the moment you finish with it, and in the case of a modal form, it definitely reaches the goal.

The second example, however, will lead to errors. The method Showreturns immediately and the form continues to display. However, the generated code usingwill immediately take the form Disposeand force it to leave. The form should be deleted only after its completion.

+6
source

All Articles