Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
C#
public interface IDisposable
{
   void Dispose();
}



------------------------------------------
C#
public class MyClass : IDisposable
{
    public void Dispose()
    {
        // Perform any object clean up here.

        // If you are inheriting from another class that
        // also implements IDisposable, don't forget to
        // call base.Dispose() as well.
    }
}


----------------------------------------

here in above what's real use of Interface Disposable. why can't the Dispose menmber directly implemented instead
we have to implemenmt Idisposable & then write functionality inside in dispose method.This looks like a long way.

First we have to create class then we have to define method name alone & then the class is finally implemneted
& then the fuctionality is to be written in Dispose method. so what's advantage here.
Posted
Updated 28-Jan-13 0:58am
v2

If your class implements the IDisposable interface, then it must provide its own implementation of the Dispose method. See Implementing IDisposable and the Dispose Pattern Properly[^].
 
Share this answer
 
v2
Not everything is Disposeable - or needs to be. It's only when it contains resources which need to be returned to the system that you need to implement Dispose, so when it it implemented, that is a good sign that you should either be calling Dispose on it when you are finished with it, or you should be creating it within a using block.

If it was always available, it would just make it harder to know if the class instances should be disposed - and most don't so they don't have a Dispose method to call.
 
Share this answer
 
 
Share this answer
 
I'd say you'll do good to read about Interfaces in general, and the uses they have.

Though it's true for the specific IDisposable Interface,
Basically: Interfaces expose and impose functionality on classes that implement them so that the class follows the Interface guidelines and then it is sure to provide. On the other hand the class using another class can rely on the fact that it knows the functionality is has to implement.

Implementing IDisposable is signaling that this class is implementing the Dispose function and is doing "what is needed" to be done inside to dispose of the objects and data and so forth that is has under it's management.

Cheers,
Edo
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900