其他分享
首页 > 其他分享> > 的对象类型是否包含受保护的虚拟Finalize方法?

的对象类型是否包含受保护的虚拟Finalize方法?

作者:互联网

C# 6.0 in a Nutshell by Joseph Albahari and Ben Albahari (O’Reilly).

Copyright 2016 Joseph Albahari and Ben Albahari, 978-1-491-92706-9.

在第100-101页上指出对象类成员是:

public class Object
{
    public Object();
    public extern Type GetType();
    public virtual bool Equals (object obj);
    public static bool Equals (object objA, object objB);
    public static bool ReferenceEquals (object objA, object objB);
    public virtual int GetHashCode();
    public virtual string ToString();
    protected virtual void Finalize(); //<-- this one
    protected extern object MemberwiseClone();
}

这促使我去检查VS的intellisense是否为我提供了任何引用实例的Finalize()方法,因为我不记得曾经见过它.

我没有成功获得继承有Finalize成员的对象(我正在尝试在函数内部访问它,但知道它已受到保护).

我检查了.NET的开放源代码,并且object.cs文件不包含Finalize方法.

我想念什么?这是作者的错误吗?

解决方法:

MSDN开始:

The C# compiler does not allow you to override the Finalize method.
Instead, you provide a finalizer by implementing a destructor for your
class. A C# destructor automatically calls the destructor of its base
class.

您必须使用〜ClassName()来实现destructor.

Object.cs是用C#编写的,因此它具有~Object()而不是Finalize().

我建议您阅读本article和本answer

Eric Lippert开始:

This feature is confusing, error-prone, and widely misunderstood. It
has syntax very familiar to users of C++, but surprisingly different
semantics. And in most cases, use of the feature is dangerous,
unnecessary, or symptomatic of a bug.
Sometimes you need to implement features that are only for experts who
are building infrastructure; those features should be clearly marked
as dangerous—not invitingly similar to features from other languages.

标签:finalizer,mscorlib,c,net
来源: https://codeday.me/bug/20191026/1939656.html