<bdo id='Lek5w'></bdo><ul id='Lek5w'></ul>
    <legend id='Lek5w'><style id='Lek5w'><dir id='Lek5w'><q id='Lek5w'></q></dir></style></legend>

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

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

      <tfoot id='Lek5w'></tfoot>

      Dispose() 方法中 GC.SuppressFinalize(this) 的目的是什么?

      时间:2023-09-13
        <tfoot id='rkA4j'></tfoot>

          <tbody id='rkA4j'></tbody>

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

          <legend id='rkA4j'><style id='rkA4j'><dir id='rkA4j'><q id='rkA4j'></q></dir></style></legend>
          <i id='rkA4j'><tr id='rkA4j'><dt id='rkA4j'><q id='rkA4j'><span id='rkA4j'><b id='rkA4j'><form id='rkA4j'><ins id='rkA4j'></ins><ul id='rkA4j'></ul><sub id='rkA4j'></sub></form><legend id='rkA4j'></legend><bdo id='rkA4j'><pre id='rkA4j'><center id='rkA4j'></center></pre></bdo></b><th id='rkA4j'></th></span></q></dt></tr></i><div id='rkA4j'><tfoot id='rkA4j'></tfoot><dl id='rkA4j'><fieldset id='rkA4j'></fieldset></dl></div>
            <bdo id='rkA4j'></bdo><ul id='rkA4j'></ul>
              • 本文介绍了Dispose() 方法中 GC.SuppressFinalize(this) 的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有以下代码:

                public void Dispose()
                {
                    if (_instance != null)
                    {
                        _instance = null;
                        // Call GC.SupressFinalize to take this object off the finalization
                        // queue and prevent finalization code for this object from
                        // executing a second time.
                        GC.SuppressFinalize(this);
                    }
                }
                

                虽然有一条注释解释了与 GC 相关的调用的目的,但我仍然不明白为什么会出现.

                Although there is a comment that explains purpose of that GC-related call, I still don't understand why it's there.

                当所有实例不存在时,对象是否注定要进行垃圾回收,例如在 using 块中使用时?

                Isn't object destined for garbage collection once all instances cease from existence, like, when used in using block?

                这将发挥重要作用的用例场景是什么?

                What's the use case scenario where this would play important role?

                推荐答案

                在实现 dispose 模式时,您还可以向调用 Dispose() 的类添加终结器.这是为了确保 Dispose() always 被调用,即使客户端忘记调用它.

                When implementing the dispose pattern you might also add a finalizer to your class that calls Dispose(). This is to make sure that Dispose() always gets called, even if a client forgets to call it.

                为了防止 dispose 方法运行两次(以防对象已经被释放),您添加 GC.SuppressFinalize(this);.该文档提供了一个示例:

                To prevent the dispose method from running twice (in case the object already has been disposed) you add GC.SuppressFinalize(this);. The documentation provides a sample:

                class MyResource : IDisposable
                {
                    [...]
                
                    // This destructor will run only if the Dispose method 
                    // does not get called.
                    ~MyResource()      
                    {
                        // Do not re-create Dispose clean-up code here.
                        // Calling Dispose(false) is optimal in terms of
                        // readability and maintainability.
                        Dispose(false);
                    }
                
                    // Implement IDisposable.
                    // Do not make this method virtual.
                    // A derived class should not be able to override this method.
                    public void Dispose()
                    {
                        Dispose(true);
                        // This object will be cleaned up by the Dispose method.
                        // Therefore, you should call GC.SupressFinalize to
                        // take this object off the finalization queue 
                        // and prevent finalization code for this object
                        // from executing a second time.
                        GC.SuppressFinalize(this);
                    }
                
                    private void Dispose(bool disposing)
                    {
                        // Check to see if Dispose has already been called.
                        if(!this.disposed)
                        {
                            // If disposing equals true, dispose all managed 
                            // and unmanaged resources.
                            if(disposing)
                            {
                                // Dispose managed resources.
                                component.Dispose();
                            }
                
                            // Call the appropriate methods to clean up 
                            // unmanaged resources here.
                            resource.Cleanup()          
                        }
                        disposed = true;         
                    }
                }
                

                这篇关于Dispose() 方法中 GC.SuppressFinalize(this) 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:垃圾收集的世代是什么? 下一篇:.NET 4.0 中的内存使用率非常高

                相关文章

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

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

                  1. <small id='DBrMF'></small><noframes id='DBrMF'>