<legend id='HND0h'><style id='HND0h'><dir id='HND0h'><q id='HND0h'></q></dir></style></legend>

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

    3. <small id='HND0h'></small><noframes id='HND0h'>

        <bdo id='HND0h'></bdo><ul id='HND0h'></ul>

      C# - 超出范围时对象是否立即销毁?

      时间:2023-09-13
        <tbody id='FJfU7'></tbody>

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

            <tfoot id='FJfU7'></tfoot>
          1. <small id='FJfU7'></small><noframes id='FJfU7'>

              • <bdo id='FJfU7'></bdo><ul id='FJfU7'></ul>
                本文介绍了C# - 超出范围时对象是否立即销毁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我可以相信一个对象在 C# 中超出范围时会被销毁并立即调用其析构函数吗?

                Can I trust that an object is destroyed and its destructor is called immediately when it goes out of scope in C#?

                我认为应该这样做,因为许多常见的编码实践(例如事务对象)都依赖于这种行为,但我不太习惯使用垃圾收集,并且对这些语言通常的行为方式知之甚少.

                I figure it should since many common coding practices (e.g. transaction objects) rely on this behaviour, but I'm not very used to working with garbage collection and have little insight to how such languages usually behave.

                谢谢.

                推荐答案

                不,.Net 因此 C# 依赖于垃圾回收内存管理.因此,在 GC 发现可以销毁对象之前,不会调用析构函数(在 .Net 中称为终结器).

                Nope, .Net and hence C# relies on a garbage collection memory management. So destructors (which in .Net is called finalizers) are not called until GC finds it proper to destroy the objects.

                另外:C# 中的大多数常规"对象没有析构函数.如果你需要析构函数模式,你应该实现 IDisposable interface处理模式.在一次性对象上,您还应该确保调用 Dispose 方法,或者使用 使用关键字或直接调用方法.

                Additionally: most "regular" objects in C# don't have destructors. If you need the destructor pattern you should implement the IDisposable interface with the Dispose Pattern. On disposable objects you should also make sure that the Dispose method gets called, either with the using keyword or directly calling the method.

                进一步(希望)澄清:确定性处置在 .Net 中很有用,例如当您需要显式释放不受 .Net 运行时管理的资源时.此类资源的示例是文件句柄、数据库连接等.这些资源在不再需要时立即释放通常很重要.因此,我们不能等待 GC 释放它们.

                To further (hopefully) clarify: deterministic disposal is useful in .Net e.g. when you need to explicitly free resources that is not managed by the .Net runtime. Examples of such resources are file handles, database connections, etc. It is usually important that these resources be freed as soon as they no longer are needed. Thus we cannot afford to wait for the GC to free them.

                为了在 .Net GC 的非确定性世界中获得确定性处置(类似于 C++ 的作用域行为),.Net 类依赖于 IDisposable 接口.借鉴Dispose Pattern,这里有一些例子:

                In order to get deterministic disposal (similar to the scope behavior of C++) in the non-deterministic world of the .Net GC, the .Net classes rely on the IDisposable interface. Borrowing from the Dispose Pattern, here are some examples:

                首先,实例化一个一次性资源,然后让对象超出范围,这将由 GC 来处理该对象:

                First, instantiating a disposable resource and then letting the object go out of scope, will leave it up to the GC to dispose the object:

                1.    {
                2.       var dr = new DisposableResource();
                3.    }
                

                为了解决这个问题,我们可以显式地处理对象:

                To fix this we can explicitly dispose the object:

                1.    {
                2.       var dr = new DisposableResource();
                3.
                4.       ...
                5.
                6.       dr.Dispose();
                7.    }
                

                但是如果第 2 行和第 6 行之间出现问题怎么办?不会调用 Dispose.为了进一步确保无论任何异常情况最终都会调用 Dispose,我们可以执行以下操作:

                But what if something goes wrong between line 2 and 6? Dispose will not be called. To further ensure that Dispose will finally be called regardless of any exceptions we can do the following:

                1.    var dr = new DisposableResource();
                2.    try
                3.    {
                4.       ...
                5.    }
                6.    finally
                7.    {
                8.       dr.Dispose();
                9.    }
                

                由于经常需要这种模式,C# 包含 using 关键字来简化事情.下面的例子等价于上面的:

                Since this pattern is often needed, C# includes the using keyword to simplify things. The following example is equivalent to the above:

                1.    using (var dr = new DisposableResource())
                2.    {
                3.       ...
                4.    }
                

                这篇关于C# - 超出范围时对象是否立即销毁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:是否可以在 C# 中创建一个真正的弱键字典? 下一篇:我是否需要在对象被孤立之前从对象中删除事件订阅?

                相关文章

                      <bdo id='niTCl'></bdo><ul id='niTCl'></ul>
                  1. <legend id='niTCl'><style id='niTCl'><dir id='niTCl'><q id='niTCl'></q></dir></style></legend>
                  2. <tfoot id='niTCl'></tfoot>
                  3. <small id='niTCl'></small><noframes id='niTCl'>

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