出于好奇,我反汇编了mscorlib.dll以检查System.Object类的实现.
我发现其中有些奇怪.
1).
public class Object {
...
protected override void Finalize(){}
...
}
基类为何在其中具有重写的方法?
2) public class Employee {
public void InstanceMethod() {
this.Finalize();
//Does not compile, can i not access protected methods of base class??
}
}
我只是想知道Object类中“受保护的Finalize”方法的用途是什么,为什么编译器会对其进行特殊处理?
解决方法:
签出MSDN到Object.Finalize:
Destructors are the C# mechanism for performing cleanup operations. Destructors provide appropriate safeguards, such as automatically calling the base type’s destructor. In C# code, Object.Finalize cannot be called or overridden.
因此,您的问题的答案将是:好吧-这是CLR内部的一部分; C#编译器在编写示例时将完成所有必需的工作:
public class Employee
{
//Finalizer, also known as "destructor"
~Employee()
{
}
}