Click to See Complete Forum and Search --> : When i should use a Dispose?


barbiomalefico
February 13th, 2004, 03:36 AM
normally when i use a form in a C# application i write this sequence of operations:

public void ShowMyDialogBox()
{
Form2 testDialog = new Form2();
testDialog.ShowDialog(this);
}


Today i have take a look to the MSDN example and i have noticed the use of Dispose


public void ShowMyDialogBox()
{
Form2 testDialog = new Form2();
testDialog.ShowDialog(this);
testDialog.Dispose();
}


my question is, what really do this function and when i should use it?

BorisKK
February 13th, 2004, 05:19 AM
Form is derived from System.ComponentModel.Component. The documentation of Component states that

It is recommended that a Component release resources explicitly by calls to its Dispose method, without waiting for automatic memory management through an implicit call to Finalize.

Usually the Dispose method is used to release unmanaged resources (like file handles, GDI handles, sockets, etc.) owned by the object without waiting for it to be garbage-collected.

pareshgh
February 14th, 2004, 01:22 AM
normally you don't use it.
when object is out of scope it is disposed !

BorisKK
February 16th, 2004, 08:41 AM
For an object to be disposed of automatically (and immediately) when it goes out of scope, it must be defined or referenced in an using statement (http://msdn.microsoft.com/library/en-us/csref/html/vclrfusingstatement.asp).