Difference in Dispose and Close

  • Thread starter Christopher Kimbell
  • Start date
C

Christopher Kimbell

The Dispose function is the result of implementing IDisposable interface, it
is used to explicitly relases unmanaged resources.
It is not uncommon for classes to implement both a Dispose and a Close
method, in many instances it is more logical to call Close rather than
Dispose. The Close method should then call Dispose.

When calling Close on a Form, you have the ability to cancel it using the
Closing event.

Chris
 
H

Herfried K. Wagner [MVP]

* "Alok said:
Thanx Chris,
But then question arises, that ideally should i call Florm.Close() or
Form.Dispose() in case i do not want to handle the closing event.

Call the form's 'Close' method. This will dispose the form, as MSDN
says.
Is there any difference in the way memory is de-allocated ??

No.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Alok,

Close works in different way depending how the form is shown.
If you show the form modaless (Show method) calling close will dispose the
form. Dispose method is called internally right away. In this case there is
no difference if you don't want to use the closing functionality.

If the form is shown as a modal dialog (ShowDialog) calling Close will hide
the form (not disposing it). This is because it is most likely that you want
to read some of the dialog controls when the dialog is dismissed. That's why
after ShowDialog returns and finish reading the form's controls you should
call Dispose do free the resources right away.

Normaly the code looks like

using(DialogForm form = new DialogForm)
{
if(DialogResult.OK == form.ShowDialog())
{
//read values
}
}


So even if you don't use Closing event it is good practice to call Close
instead of Dispose. This will quarantee form's correct work in either modal
and modaless scenarios
 
H

Herfried K. Wagner [MVP]

* "Alok said:
But then,
why does the form have both methods exposed i.e. close also as well as
Dispose ??
i just want to know if there are any differences in the wayboth should be
handled.

'Form' inherits from 'Control', and 'Control' inherits from
'System.ComponentModel.Component', and this class implements
'IDisposable'.
 
A

Alok

Can anybody tell me the difference between
Form.Dispose() and Form.Close() ??

It would also be helpful in terms of performance et al...

TIA :)
 
A

Alok

Thanx Chris,
But then question arises, that ideally should i call Florm.Close() or
Form.Dispose() in case i do not want to handle the closing event.

Is there any difference in the way memory is de-allocated ??

TIA :)
 
A

Alok

But then,
why does the form have both methods exposed i.e. close also as well as
Dispose ??
i just want to know if there are any differences in the wayboth should be
handled.

TIA :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top