Basics...

  • Thread starter Thread starter DC
  • Start date Start date
D

DC

Hi!

Is code like:

<code>
FormX f = new FormX();
f.ShowDialog();
f.Dispose();
GC.Collect();
</code>

bad thing or good thing?


Thanks,
Dejan
 
DC said:
Is code like:

<code>
FormX f = new FormX();
f.ShowDialog();
f.Dispose();
GC.Collect();
</code>

bad thing or good thing?

it's useless I'd say. IMHO you only have to dispose of unmanaged
resources if you want to free memory. Disposing of a form which is not
in use anymore and out of scope is done automatically as soon as memory
is needed. f.Dispose() and GC.Collect() are therefore not needed unless
your form does some dirty things and overrides the Dispose() method of
the base class.
 
Konrad L. M. Rudolph said:
it's useless I'd say. IMHO you only have to dispose of unmanaged
resources if you want to free memory. Disposing of a form which is not
in use anymore and out of scope is done automatically as soon as memory
is needed. f.Dispose() and GC.Collect() are therefore not needed unless
your form does some dirty things and overrides the Dispose() method of
the base class.

Windows handles are not an infinite resource, and not one which the
garbage collector keeps track of. While I agree that the call to
GC.Collect() isn't needed, you should *always* make sure that classes
which implement IDisposable have their Dispose methods called. There's
more to unmanaged resources than just memory!

I would, however, change the format of the code to:

using (FormX f = new FormX())
{
f.ShowDialog();
}
 
Back
Top