问题描述
我可以相信一个对象在 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# - 超出范围时对象是否立即销毁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!