locked
Are all classes in .NET implement IDisposable interface.? RRS feed

  • Question

  • Hi All,

    I know, I am too late to know/learn about the "using" clause in .NET.

    I recently learned that "using" clause helps to improve our code by automatically calling the Dispose method of at the end of the using block.

    My question here is that do all classes directly/indirectly implement the IDisposable interface.?

    If "No" is the answer for above question,What's the purpose of the "using" clause in such a scenario.


    Thanks, Satish Bommideni
    Wednesday, December 14, 2011 11:00 AM

Answers

  • No - All classes do not impplement IDisposable.

    And you can use "using" on an instance only if it implements IDisposable. For example, System.IO.FileStream class implements IDisposable. So, you can using like,

    using (FileSystem fs = new FileSystem (..))
    {
         ...
    }
    

    But, System.Collections.ArrayList class doesn't implement IDisposable. Hence if you try to use using block, it will give you compile error saying 'System.Collections.ArrayList': type used in a using statement must be implicitly convertible to 'System.IDisposable'

    using (System.Collections.ArrayList aList = new System.Collections.ArrayList())
    {
    }
    


     


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Proposed as answer by Horizon_Net Wednesday, December 14, 2011 12:57 PM
    • Marked as answer by Spartan_578 Thursday, December 15, 2011 4:15 AM
    Wednesday, December 14, 2011 11:08 AM

All replies

  • No - All classes do not impplement IDisposable.

    And you can use "using" on an instance only if it implements IDisposable. For example, System.IO.FileStream class implements IDisposable. So, you can using like,

    using (FileSystem fs = new FileSystem (..))
    {
         ...
    }
    

    But, System.Collections.ArrayList class doesn't implement IDisposable. Hence if you try to use using block, it will give you compile error saying 'System.Collections.ArrayList': type used in a using statement must be implicitly convertible to 'System.IDisposable'

    using (System.Collections.ArrayList aList = new System.Collections.ArrayList())
    {
    }
    


     


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Proposed as answer by Horizon_Net Wednesday, December 14, 2011 12:57 PM
    • Marked as answer by Spartan_578 Thursday, December 15, 2011 4:15 AM
    Wednesday, December 14, 2011 11:08 AM
  • Hi,

    See http://msdn.microsoft.com/en-us/library/system.idisposable.aspx for  details.

    In short : they don't implement all IDisposable whose purpose is to release unmanaged resources. And using is not usable with a class that doesn't implement IDisposable...

    In rare cases I've seen IDisposable being implement to benefit from the using pattern. For example a class that would emit begin and end tags could use this so that the developer can use using to automatically emit the end tag...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Wednesday, December 14, 2011 11:12 AM
  • In rare cases I've seen IDisposable being implement to benefit from the using pattern. For example a class that would emit begin and end tags could use this so that the developer can use using to automatically emit the end tag...

    Examples of this are some 'Transaction' classes for use in database calls.  It's not that they're holding unmanaged resources, it's so that the developer can just put the transaction in a using clause.  The commit code can then be placed in the dispose method.  It's actually kinda slick, although it can be a bit confusing since it's using the 'using' statement in a manor that it wasn't really intended for.
    Wednesday, December 14, 2011 2:40 PM