对象类型是否包含受保护的虚拟 Finalize 方法?
Does the object type contain a protected virtual Finalize method?
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 页指出,对象 class 成员是:
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's intellisense
是否为任何参考实例提供了一个 Finalize()
方法,因为我不记得曾经见过一个。
我没有成功获得这样一个继承了 Finalize
成员的对象(我试图在函数内部访问它,意识到它是受保护的)。
我检查了 .NET's open source code
并且 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 and this 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.
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 页指出,对象 class 成员是:
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's intellisense
是否为任何参考实例提供了一个 Finalize()
方法,因为我不记得曾经见过一个。
我没有成功获得这样一个继承了 Finalize
成员的对象(我试图在函数内部访问它,意识到它是受保护的)。
我检查了 .NET's open source code
并且 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 and this 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.