Question on Dispose / IDispose

  • Thread starter Thread starter Cider123
  • Start date Start date
C

Cider123

Ran across some code on the net that incorporates "dispose" 2
different ways..

I understand a calling routine could use the .Dispose on your class,
but I was under the impression "Dispose()" was the same thing as the
deconstructor in the following example (~YourClassNameHere()).

What would cause the deconstructor to fire off over the "Dispose()"
routine?

Would the deconstructor get called first, and then the Dispose() last?
If so, why not just have one simple "Dispose()" routine?




Snapshot of the Code:

internal class YourClassNameHere : MarshalByRefObject, IDisposable

~YourClassNameHere()
{
dispose( false );
}

public void Dispose()
{
dispose( true );
}

private void dispose( bool disposing )
{
if( disposing )
{
a = null;
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.GC.Collect( 0 );
}
}
 
The "deconstructor" or as its known in .NET, the Finalizer, has a similar function to Dispose but different semantics and usage pattern. Rather than write a huge tract on this I'll refer you to Chris Lyon's excellent description of their relationship

http://blogs.msdn.com/clyon/archive/2004/09/21/232445.aspx

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Ran across some code on the net that incorporates "dispose" 2
different ways..

I understand a calling routine could use the .Dispose on your class,
but I was under the impression "Dispose()" was the same thing as the
deconstructor in the following example (~YourClassNameHere()).

What would cause the deconstructor to fire off over the "Dispose()"
routine?

Would the deconstructor get called first, and then the Dispose() last?
If so, why not just have one simple "Dispose()" routine?




Snapshot of the Code:

internal class YourClassNameHere : MarshalByRefObject, IDisposable

~YourClassNameHere()
{
dispose( false );
}

public void Dispose()
{
dispose( true );
}

private void dispose( bool disposing )
{
if( disposing )
{
a = null;
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.GC.Collect( 0 );
}
}

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.770 / Virus Database: 517 - Release Date: 27/09/2004



[microsoft.public.dotnet.languages.csharp]
 
Back
Top