1. <legend id='y0I37'><style id='y0I37'><dir id='y0I37'><q id='y0I37'></q></dir></style></legend>
      • <bdo id='y0I37'></bdo><ul id='y0I37'></ul>

      <small id='y0I37'></small><noframes id='y0I37'>

    2. <tfoot id='y0I37'></tfoot>
        <i id='y0I37'><tr id='y0I37'><dt id='y0I37'><q id='y0I37'><span id='y0I37'><b id='y0I37'><form id='y0I37'><ins id='y0I37'></ins><ul id='y0I37'></ul><sub id='y0I37'></sub></form><legend id='y0I37'></legend><bdo id='y0I37'><pre id='y0I37'><center id='y0I37'></center></pre></bdo></b><th id='y0I37'></th></span></q></dt></tr></i><div id='y0I37'><tfoot id='y0I37'></tfoot><dl id='y0I37'><fieldset id='y0I37'></fieldset></dl></div>

        终结器和 IDisposable

        Finalizer and IDisposable(终结器和 IDisposable)

            <tfoot id='qSlLB'></tfoot><legend id='qSlLB'><style id='qSlLB'><dir id='qSlLB'><q id='qSlLB'></q></dir></style></legend>

                <tbody id='qSlLB'></tbody>
              • <small id='qSlLB'></small><noframes id='qSlLB'>

                  <bdo id='qSlLB'></bdo><ul id='qSlLB'></ul>
                  <i id='qSlLB'><tr id='qSlLB'><dt id='qSlLB'><q id='qSlLB'><span id='qSlLB'><b id='qSlLB'><form id='qSlLB'><ins id='qSlLB'></ins><ul id='qSlLB'></ul><sub id='qSlLB'></sub></form><legend id='qSlLB'></legend><bdo id='qSlLB'><pre id='qSlLB'><center id='qSlLB'></center></pre></bdo></b><th id='qSlLB'></th></span></q></dt></tr></i><div id='qSlLB'><tfoot id='qSlLB'></tfoot><dl id='qSlLB'><fieldset id='qSlLB'></fieldset></dl></div>
                • 本文介绍了终结器和 IDisposable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  基于文档(MSDN:链接),很明显,在实现终结器时应该使用 IDisposable 模式.

                  Based on the documentation (MSDN: link), it is clear that one should use the IDisposable pattern when implementing a finalizer.

                  但是,如果您实现了 IDisposable(以便提供一种确定性的对象处置方式),并且您没有任何非托管资源需要清理,您是否需要实现终结器?

                  But do you need to implement a finalizer if you implement IDisposable (so as to provide a deterministic way of disposing the object), and you dont have any unmanaged resources to clean up?

                  在我看来,如果该类只有托管资源并且您不调用 Dispose,则托管资源将自动被 GC 清理,因此无需实现终结器.我错了吗?

                  As I see it, if the class has only managed resources and if you dont call Dispose, the managed resources will automatically get cleaned up by the GC and hence no need to implement the finalizer. Am I wrong?

                  另外,如果我使用我的 Dispose 方法来清理事件处理程序会怎样.由于 Dispose 不会自动被 GC 调用,我是否应该实现一个终结器,以确保事件处理程序不连线?

                  Also, what if I am using my Dispose method to clean up event handlers. As Dispose wont automatically get called by the GC, should I implement a Finalizer, to ensure that eventhandlers get unwired?

                  推荐答案

                  不,如果你有一个实现 IDisposable 的类,你不需要实现终结器(也就是说,如果你已经正确实现了模式,并且你只已管理的资源进行处置).

                  No, you do not need to implement a finalizer if you have a class that implements IDisposable (that is if you have implemented the pattern correctly, and that you only have managed resources to dispose of).

                  (如果这样做,它实际上会影响对象的生命周期,因为带有终结器的对象会被添加到 GC 中的终结队列中,并且可以比它们需要的寿命更长 - 如果你的对象很大.)

                  这篇关于终结器和 IDisposable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C# namespace alias - what#39;s the point?(C# 命名空间别名 - 有什么意义?)
                  Using Xpath With Default Namespace in C#(在 C# 中使用具有默认命名空间的 Xpath)
                  Generating an EDMX from a DB2 Database(从 DB2 数据库生成 EDMX)
                  IBM .NET Data Provider Connection String issue with Library List(库列表的 IBM .NET 数据提供程序连接字符串问题)
                  .NET DB2 OLEDB pre-requisites(.NET DB2 OLEDB 先决条件)
                  Referring to Code in IBM.Data.DB2 makes that Assembly Unavailable to the rest of my Solution(引用 IBM.Data.DB2 中的代码使该程序集对我的解决方案的其余部分不可用)

                  <small id='plLpV'></small><noframes id='plLpV'>

                  <tfoot id='plLpV'></tfoot>
                      <bdo id='plLpV'></bdo><ul id='plLpV'></ul>
                      <legend id='plLpV'><style id='plLpV'><dir id='plLpV'><q id='plLpV'></q></dir></style></legend>
                          <tbody id='plLpV'></tbody>
                            <i id='plLpV'><tr id='plLpV'><dt id='plLpV'><q id='plLpV'><span id='plLpV'><b id='plLpV'><form id='plLpV'><ins id='plLpV'></ins><ul id='plLpV'></ul><sub id='plLpV'></sub></form><legend id='plLpV'></legend><bdo id='plLpV'><pre id='plLpV'><center id='plLpV'></center></pre></bdo></b><th id='plLpV'></th></span></q></dt></tr></i><div id='plLpV'><tfoot id='plLpV'></tfoot><dl id='plLpV'><fieldset id='plLpV'></fieldset></dl></div>