Proper use of the IDisposable interface

后端 未结 19 3260
情深已故
情深已故 2020-11-21 04:05

I know from reading the Microsoft documentation that the \"primary\" use of the IDisposable interface is to clean up unmanaged resources.

To me, \"unman

19条回答
  •  广开言路
    2020-11-21 04:59

    In the example you posted, it still doesn't "free the memory now". All memory is garbage collected, but it may allow the memory to be collected in an earlier generation. You'd have to run some tests to be sure.


    The Framework Design Guidelines are guidelines, and not rules. They tell you what the interface is primarily for, when to use it, how to use it, and when not to use it.

    I once read code that was a simple RollBack() on failure utilizing IDisposable. The MiniTx class below would check a flag on Dispose() and if the Commit call never happened it would then call Rollback on itself. It added a layer of indirection making the calling code a lot easier to understand and maintain. The result looked something like:

    using( MiniTx tx = new MiniTx() )
    {
        // code that might not work.
    
        tx.Commit();
    } 
    

    I've also seen timing / logging code do the same thing. In this case the Dispose() method stopped the timer and logged that the block had exited.

    using( LogTimer log = new LogTimer("MyCategory", "Some message") )
    {
        // code to time...
    }
    

    So here are a couple of concrete examples that don't do any unmanaged resource cleanup, but do successfully used IDisposable to create cleaner code.

提交回复
热议问题