views:

138

answers:

1

I understand the concept and reasons behind using the using statement, and I use it with things like file resources and remote connections, I was wondering if it is good practice to use the using statement with WinForm forms and dialogs?

using (MyDialog dlg = new MyDialog())
{
    if (dlg.ShowDialog() == EDialogResult.OK)
    {
        // Do Something
    }
}

Thanks!

+4  A: 

With Dialogs only. But then it is a very good practice.

You will find that it doesn't work around Show(), because using(){} can only be used inside 1 method and you never want to Close again right after a Show().

Henk Holterman
Of course that makes sense in regards to only using with dialogs. :)
Nate Heinrich
I dont understand why you'd need a Dispose with ShowDialog while with Show you dont.. creates some inconsistency in my code every time. i usually also add a using around ShowDialog, but only to make my profiler not list it as disposable instance that hasn't been disposed. is there a real reason why it should be disposed?
stmax
@stmax it depends on the scope of the form being shown. ShowDialog will 'block' until the form is closed so the using block wont terminate until after the form has closed. Show on the other hand returns 'instantly' so you would end up disposing an object that is still in use.
Courtney de Lautour