none
Dispose - when and where?

    Question

  • I tried to Google Dispose and I got a HUGE variance in responses. From "always" to "never" to "everything in between" and all from reputable sites.

    I tried to search it in the MSDN forums here, but I didn't get good enough answers. All I found where stuff like "All streams are disposed on close so don't worry" and "Connections are put in pools, but you may not want to dispose since the pool can re-use connections".

    Can anyone recommend a good reading that touches on the different classes and how they interact with dispose and how to properly use dispose for your own classes?

     

    Thanks  :-)

    Thursday, May 06, 2010 2:27 PM

Answers

  • Looking back at the design of the Framework, we actually regret adding the concept of both Close and Dispose. Our recommendations going forward for new classes is to have only Dispose. If we could start again, there would be no Stream.Close, only Stream.Dispose. To back up these recommendations, in .NET 2.0, we changed a bunch of BCL classes so that Dispose and Close meant exactly the same thing*.

    * The exception to this is SqlConnection, et al, Close does not mean the same as Dispose. Close closes the connection, but allows you reuse the same connection instance later by calling Open. Dispose closes the connection, but does not allow you to reuse the same connection instance.

    The way I see it, is if you created the resources (ie via new) or were given ownership of the resource (either handed to you or created via a factory method), always call Dispose when you are finished with it.


    Base Class Library Team (BCL) | My Blog: http://davesbox.com
    • Marked as answer by Benjie2525 Tuesday, May 25, 2010 2:11 PM
    Thursday, May 06, 2010 3:12 PM
    Moderator

All replies

  • Here's my recommendations on when to implement Dispose:
      http://nitoprograms.blogspot.com/2009/08/how-to-implement-idisposable-and.html

    If you also need a recommendation on when to invoke Dispose, then I would say "always". In a using statement, if possible. If it's a shared resource (where you can't use using), then use the RefCountDisposable from Microsoft's Rx library.

           -Steve

    P.S. Disposing DB connections by default will return them to the pool so they can be reused. This is an unusual implementation of Dispose, since it does not actually release the resources.

    P.P.S. "Close" is the same as "Dispose" for any Microsoft class.


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible
    • Marked as answer by Benjie2525 Thursday, May 06, 2010 3:27 PM
    • Unmarked as answer by Benjie2525 Tuesday, May 25, 2010 2:10 PM
    Thursday, May 06, 2010 2:46 PM
  • Looking back at the design of the Framework, we actually regret adding the concept of both Close and Dispose. Our recommendations going forward for new classes is to have only Dispose. If we could start again, there would be no Stream.Close, only Stream.Dispose. To back up these recommendations, in .NET 2.0, we changed a bunch of BCL classes so that Dispose and Close meant exactly the same thing*.

    * The exception to this is SqlConnection, et al, Close does not mean the same as Dispose. Close closes the connection, but allows you reuse the same connection instance later by calling Open. Dispose closes the connection, but does not allow you to reuse the same connection instance.

    The way I see it, is if you created the resources (ie via new) or were given ownership of the resource (either handed to you or created via a factory method), always call Dispose when you are finished with it.


    Base Class Library Team (BCL) | My Blog: http://davesbox.com
    • Marked as answer by Benjie2525 Tuesday, May 25, 2010 2:11 PM
    Thursday, May 06, 2010 3:12 PM
    Moderator
  • Thanks guys for the insight  :-)

     

    off to read that article.

     

    edit: I do see you both seem to disagree with Connection on how it handles close/dispose. It doesn't hugely matter since connection pooling is useful and I'll just stick with close, but I am curious as to the difference. Did a change occur between .Net versions on how this is handled?

    • Edited by Benjie2525 Thursday, May 06, 2010 3:31 PM connections
    Thursday, May 06, 2010 3:28 PM
  • I would expect David is correct.

    The MSDN docs for DbConnection.Close notes that Close and Dispose are equivalent for db connections, and that they both release them to the connection pool (or if there's no connection pool, they're just closed). Likewise, SQL Server Connection Pooling states "Connections are released back into the pool when they are closed or disposed." Similar language is in OLE DB, ODBC, and Oracle Pooling.

    However, looking at SqlConnection in Reflector, Close and Dispose are not the same.

    I'm a smart guy (IMO), but David works for Microsoft. Source access trumps all other knowledge. ;)

           -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible
    Saturday, May 08, 2010 1:16 PM