IDisposable Interface

R

rajesh

I am new to .NET programming and OOPS. I am interested in knowing
about Dispose method. They are

+ What are the circumstances under which we should implement the
IDisposable interface.
+ What would I loose if do not implement the IDisposable interface.
+ I am invoking instances of certain custom classes in my class. If I
simply implement the IDisposable interface, then would all these
instances be disposed
automatically killed? Or do I need to set them to nothing explicitly?
If I need to set them to nothing explicitly, then in which method
should I set them to
nothing?

Please help.

regards,
Rajesh
 
C

Cor

Hi Rajesh,

dotNet is managed code, the documentation on MSDN is huge, when there is
not written that you need to implement Idisposable, do not do it. The same
is with setting an object to nothing, when there is a close method, that is
enough in my opinion.

Cor
 
R

Ron McNulty

Hi Rajesh

You should implement the IDisposable interface when there are OS resources
involved. The dispose() method should release those resources (close files,
close database connections etc). That way your program will not hog
resources waiting on the GC to do its work.

I'm not sure about .NET, but I once noticed a java GC fired 8 minutes after
the object was eligible for collection!

Regards

Ron
 
M

Miha Markic [MVP C#]

Hi,

I am new to .NET programming and OOPS. I am interested in knowing
about Dispose method. They are

+ What are the circumstances under which we should implement the
IDisposable interface.

You have to implement it when unmanaged resources are involved.
You might implement it when you need something to be clean out on Dispose -
it is up to you.
+ What would I loose if do not implement the IDisposable interface.

You might leave unmanaged resources used forever ;-)
+ I am invoking instances of certain custom classes in my class. If I
simply implement the IDisposable interface, then would all these
instances be disposed
automatically killed? Or do I need to set them to nothing explicitly?
If I need to set them to nothing explicitly, then in which method
should I set them to
nothing?

By implementing IDisposable you only show that Dispose should be called to
clear up resources used.
Implementators should call Dispose when the class is no longer needed.
Setting the instance to nothing has no effect whatsoever other then the
instance can be gc-ed sooner.
 
R

rajesh

Dear all,
Thanks for the useful information. This gives me a much better picture
of this interface. Thanks again
Regards,
Rajesh
 

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