Click here to Skip to main content
11,412,516 members (66,014 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
public interface IDisposable
{
   void Dispose();
}


------------------------------------------
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 28-Jan-13 1:28am
Edited 28-Jan-13 1:58am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^].
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 364
1 Sascha Lefévre 200
2 Maciej Los 135
3 ProgramFOX 130
4 Sergey Alexandrovich Kryukov 110
0 Sergey Alexandrovich Kryukov 9,025
1 OriginalGriff 7,247
2 Maciej Los 3,525
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web04 | 2.8.150427.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100