Finalizers with Dispose() in C#

前端 未结 2 2014
情歌与酒
情歌与酒 2021-01-06 03:45

See the code sample from MSDN: (http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=VS.100).aspx)

// Design pattern for a base class.
public class Base: IDisp         


        
相关标签:
2条回答
  • 2021-01-06 04:20

    If someone forgets to call Dispose, the finalizer will (eventually) run to do final cleanup. Since finalization hurts performance, ideally no-one will forget to Dispose. The using construct helps a little with that.

    0 讨论(0)
  • 2021-01-06 04:25

    There are 2 scenarios:

    • Your code calls Dispose (preferred) and the Finalizer is canceled, eliminating the overhead.
    • Your code 'leaks' the object and the GC calls the Finalizer.
    0 讨论(0)
提交回复
热议问题