Sub Dispose or Implementing the Dispose Interface?


Scott M.

In a typical class, do I need to indicate that it implements the IDisposable
interface and then create a Dispose method that implements the Dispose
required by the IDisposable interface or can I just make a Sub Dispose() and
the CLR will know that this is the Dispose method to call when the object
falls out of scope?




Imran Koradia

The runtime (CLR) never calls the dispose method. It is upto the client
using the object to call dispose. There are a couple of exceptions. Read
this for more information:

So whether you just add a Dispose method or implement IDisposable interface,
its upto the application using your class to call the dispose method.
Ofcourse, it makes more sense to implement IDisposable since that is indeed
why the interface is there - for one to write dispose code. If you simply
write a Sub Dispose, it could be misleading to someone using your class
since one would expect an implementation of IDisposable if one were to
follow the standards.

hope that helps..

Jay B. Harlow [MVP - Outlook]

The framework itself never calls Dispose.

C# will call IDisposable.Dispose if you use the object in a Using statement.

VB.NET 2002 & 2003 & C#'s For Each statements will call IDisposable.Dispose
if its part of a class that implements IEnumerator.

Otherwise VB.NET (2002 & 2003) will not call IDisposable.Dispose.

VB.NET 2005 (aka Whidbey, due out later in 2005) will add support to VB.NET
for the Using statement, the Using statement will call IDisposable.Dispose
on the End Using line.

Information on VB.NET 2005:

Information on VB.NET's Using statement:

An object falling out of scope never calls Dispose nor IDisposable.Dispose.

If you are considering implementing IDisposable, you should also consider
implementing Finalize also.

The "Implementing Finalize and Dispose to Clean Up Unmanaged Resources" on

Covers when you should implement Finalize, when you should implement both,
and when you should implement just IDisposable.

Hope this helps

Scott M.

Ok Imran, then let me ask you this...

When the GC does eventually collect the garbage, what determines which
objects are collected and which are not? Is is simply that any object that
is no longer referenced are the ones collected?

Cor Ligthert


When you use the componenttemplate to create the class, it gives you a
complete class where IDisposable setting is completly done for you.

In any class that implements IDisposable is the disposing from the resourses
used in that class done for you.


Larry Serflaten

Scott M. said:
Ok Imran, then let me ask you this...

When the GC does eventually collect the garbage, what determines which
objects are collected and which are not? Is is simply that any object that
is no longer referenced are the ones collected?

Its not quite that simple. The GC is good at cleaning up short lived objects,
but those that hang around for a while get shoved on 'the back burner' (meaning
they don't get tested as often).

The GC is also pretty good at managing those long lived objects, but its the
middle ground that needs to be addressed. That area is dependant on your
own use of memory, so just how often the GC runs, and decides to move
things to the 'back burner' is partially dependant on what you're doing....

The proper term is 'generations'. Generation 0 items come and go as needed....

So, its those Gen 0 items that are swept away when nothing holds a reference
to them. If they survive that first look, then get shoved into the Gen 1 area, and
then Gen 2, etc where they don't go looking for memory as often as the Gen 0 area.

For a bit more on the GC, you might want to watch the .Net show where one
of the CLR's architect was interviewed:




Imran Koradia


As Larry mentioned, collection is a bit more complicated. I think you are
confusing a finalize method with the dispose method (I could be wrong
though). If an object has a finalizer, it is not collected (when a
collection is performed by the GC and it finds that the object is not
referenced anymore) but instead is pushed onto a finalization queue so that
the runtime can execute the finalize method of the object. And then there is
the concept of various generations (0, 1 and 2 in the current framework
version). I would suggest you read these 2 excellent articles by Jeffrey
Richter on Garbage collection which also talks about finalization and

Also, here is a blog post on dispose dos and don'ts by Chris Lyon which you
might be interested in since you are deciding to implement dispose in your

hope that helps..


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