在 C# 中,类中的析构函数和 Finalize 方法有什么区别?
类中的析构函数和 Finalize 方法之间有什么区别(如果有)?
我最近发现 Visual Studio 2008 认为析构函数与 Finalize 方法同义,这意味着 Visual Studio 不允许您在类中同时定义这两种方法。
例如,以下代码片段:
class TestFinalize
{
~TestFinalize()
{
Finalize();
}
public bool Finalize()
{
return true;
}
}
在析构函数中调用 Finalize 时出现以下错误:
以下方法或属性之间的调用不明确: 'TestFinalize.~TestFinalize()' 和 'TestFinalize.Finalize()'
如果对 Finalize 的调用被注释掉,则会出现以下错误:
类型“ManagementConcepts.Service.TestFinalize”已定义一个名为的成员 使用相同的参数类型“最终确定”
What is the difference, if there is one, between a destructor and a Finalize method in a class?
I recently discovered that Visual Studio 2008 considers a destructor synonymous with a Finalize method, meaning that Visual Studio won't let you simultaneously define both methods in a class.
For example, the following code fragment:
class TestFinalize
{
~TestFinalize()
{
Finalize();
}
public bool Finalize()
{
return true;
}
}
Gives the following error on the call to Finalize in the destructor:
The call is ambiguous between the following methods or properties:
'TestFinalize.~TestFinalize()' and 'TestFinalize.Finalize()'
And if the call to Finalize is commented out, it gives the following error:
Type 'ManagementConcepts.Service.TestFinalize' already defines a member called
'Finalize' with the same parameter types
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C# 中的析构函数会重写 System.Object.Finalize 方法。 您必须使用析构函数语法来执行此操作。 手动覆盖
Finalize
将会给您一条错误消息。基本上,您尝试使用
Finalize
方法声明执行的操作是 隐藏基类的方法。 它将导致编译器发出警告,可以使用 new 修饰符将其静音(如果它可以工作)。 这里需要注意的重要一点是,您不能同时覆盖
并声明具有相同名称的新
成员,因此同时拥有析构函数和Finalize
方法将导致错误(但是您可以声明一个public new void Finalize()
方法,尽管不建议这样做如果您没有声明析构函数)。A destructor in C# overrides
System.Object.Finalize
method. You have to use destructor syntax to do so. Manually overridingFinalize
will give you an error message.Basically what you are trying to do with your
Finalize
method declaration is hiding the method of the base class. It will cause the compiler to issue a warning which can be silenced using thenew
modifier (if it was going to work). The important thing to note here is that you can't bothoverride
and declare anew
member with identical name at the same time so having both a destructor and aFinalize
method will result in an error (but you can, although not recommended, declare apublic new void Finalize()
method if you're not declaring a destructor).维基百科对终结器和析构函数之间的区别有一些很好的讨论。 href="http://en.wikipedia.org/wiki/Finalizer" rel="noreferrer">终结器文章。
C# 确实没有“真正的”析构函数。 语法类似于 C++ 析构函数,但它实际上是终结器。 您在示例的第一部分中正确地编写了它:
上面是
Finalize
函数的语法糖。 它确保基础中的终结器能够运行,但在其他方面与覆盖 Finalize 函数相同。 这意味着当您编写析构函数语法时,您实际上是在编写终结器。根据 Microsoft 的说法,终结器是指垃圾收集器调用的函数当它收集时 (
Finalize
),而析构函数是作为结果执行的一段代码(成为Finalize
的语法糖)。 它们非常接近,微软根本不应该区分它们。Microsoft 使用 C++ 的“析构函数”术语具有误导性,因为在 C++ 中,一旦对象被删除或从堆栈中弹出,它就会在同一线程上执行,而在 C# 中,它会在另一时间在单独的线程上执行。
Wikipedia has some good discussion on the difference between a finalizer and a destructor in the finalizer article.
C# really doesn't have a "true" destructor. The syntax resembles a C++ destructor, but it really is a finalizer. You wrote it correctly in the first part of your example:
The above is syntactic sugar for a
Finalize
function. It ensures that the finalizers in the base are guaranteed to run, but is otherwise identical to overriding theFinalize
function. This means that when you write the destructor syntax, you're really writing the finalizer.According to Microsoft, the finalizer refers to the function that the garbage collector calls when it collects (
Finalize
), while the destructor is your bit of code that executes as a result (the syntactic sugar that becomesFinalize
). They are so close to being the same thing that Microsoft should have never made the distinction.Microsoft's use of the C++'s "destructor" term is misleading, because in C++ it is executed on the same thread as soon as the object is deleted or popped off the stack, while in C# it is executed on a separate thread at another time.
在这里找到:http://sanjaysainitech.blogspot.com/2007/ 06/destructor-dispose.html之间的差异
更新: 这里有有趣的半相关线程。
Found here: http://sanjaysainitech.blogspot.com/2007/06/difference-between-destructor-dispose.html
Update: Interesting semi-related thread here.