C# Form.Close vs Form.Dispose
C#WinformsC# Problem Overview
I am new to C#, and I tried to look at the earlier posts but did not find a good answer.
In a C# Windows Form Application with a single form, is using Form.Close()
better or Form.Dispose()
?
MSDN says that all resources within the object are closed and the form is disposed when a Close is invoked. Inspite of which, I have come across several examples online which follow a Dispose rather than a Close.
Does one have an advantage over the other? Under which scenarios should we prefer one over the other?
C# Solutions
Solution 1 - C#
This forum on MSDN tells you.
> Form.Close()
sends the proper Windows
> messages to shut down the win32
> window. During that process, if the
> form was not shown modally, Dispose is
> called on the form. Disposing the form
> frees up the unmanaged resources that
> the form is holding onto.
>
> If you do a form1.Show()
or
> Application.Run(new Form1())
, Dispose
> will be called when Close()
is called.
>
> However, if you do form1.ShowDialog()
> to show the form modally, the form
> will not be disposed, and you'll need
> to call form1.Dispose()
yourself. I
> believe this is the only time you
> should worry about disposing the form
> yourself.
Solution 2 - C#
As a general rule, I'd always advocate explicitly calling the Dispose method for any class that offers it, either by calling the method directly or wrapping in a "using" block.
Most often, classes that implement IDisposible do so because they wrap some unmanaged resource that needs to be freed. While these classes should have finalizers that act as a safeguard, calling Dispose will help free that memory earlier and with lower overhead.
In the case of the Form object, as the link fro Kyra noted, the Close method is documented to invoke Dispose on your behalf so you need not do so explicitly. However, to me, that has always felt like relying on an implementaion detail. I prefer to always call both Close and Dispose for classes that implement them, to guard against implementation changes/errors and for the sake of being clear. A properly implemented Dispose method should be safe to invoke multiple times.
Solution 3 - C#
Not calling Close
probably bypasses sending a bunch of Win32 messages which one would think are somewhat important though I couldn't specifically tell you why...
Close
has the benefit of raising events (that can be cancelled) such that an outsider (to the form) could watch for FormClosing
and FormClosed
in order to react accordingly.
I'm not clear whether FormClosing
and/or FormClosed
are raised if you simply dispose the form but I'll leave that to you to experiment with.
Solution 4 - C#
Using using
is a pretty good way:
using (MyForm foo = new MyForm())
{
if (foo.ShowDialog() == DialogResult.OK)
{
// your code
}
}
Solution 5 - C#
Close() - managed resource can be temporarily closed and can be opened once again.
Dispose() - permanently removes managed or not managed resource
Solution 6 - C#
An old question, having some good answers, but I want to share a clear answer for this very popular question
How do I close Form in code?
-
Did you open the form using
Show
?Then to close it in code, just call
Close()
. -
Did you open the form using
ShowDialog
?Then close it by
Close
or by settingDialogResult
.
Do I need to dispose Form?
-
Did you open the form using
Show
?No you don't need. After you close the form by
Close
or by clicking on X, it will be disposed automatically. -
Did you open the form using
ShowDialog
?Yes you need. After you close the form by
Close
or by settingDialogResult
or by clicking on X, if you are not going to reuse the form, you need to explicitly callDispose
, or make sure you create the form instance in ausing
block like this://form will be disposed after the using block using (var f = new MyForm()) { if (f.ShowDialog() == DialogResult.OK) { //Your logic to handle OK here } }
If you want to reuse it later, then do not forget to explicitly
Dispose
in when you no more need it.
Solution 7 - C#
If you use form.close() in your form and set the FormClosing Event of your form and either use form.close() in this Event ,you fall in unlimited loop and Argument out of range happened and the solution is that change the form.close() with form.dispose() in Event of FormClosing. I hope this little tip help you!!!
Solution 8 - C#
What I have just experiment with VS diagnostic tools is I called this.Close() then formclosing event triggered. Then When I call this.Dispose() at the end in Formclosing event where I dispose many other objects in it, it cleans everything much much smoother.