为什么定案方法不能覆盖 [英] Why Finalize method not allowed to override

查看:175
本文介绍了为什么定案方法不能覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的.NET ..和我很困惑,在C#中的析构函数机制..please澄清

在C#中的析构函数转换由CLR来完成的方法。 如果我们试图重写它(不使用析构函数),会得到一个错误 错误2不覆盖object.Finalize。相反,提供了析构函数。

但似乎在mscorlib.dll对象CALSS实现已敲定定义为保护覆盖无效的Finalize(){},那么为什么不能,我们将其覆盖,什么虚函数。

为什么这样设计,就是它是用C ++析构函数的概念是一致的。

此外,当我们去的对象类的定义,也没有提及的finalize方法,那么请问该怎么hmscorlib.dll定义显示的finalize函数。 这是否意味着默认的析构函数转换成最后确定的方法。

 公共类对象
{

    公共对象();
    公共虚拟布尔等于(obj对象);
    公共静态布尔等于(对象objA,对象objB);
    公共虚拟INT GetHash code();
    公共类型的GetType();
    保护对象MemberwiseClone();
    公共静态布尔的ReferenceEquals(对象objA,对象objB);
    公共虚拟字符串的ToString();
}
 

解决方案
  

是新来的.NET,我很困惑,在C#中的析构函数的机制。请澄清。

当然。我同意这是混乱的。

  

在C#中的析构函数转换由CLR来完成的方法。

正确的。好吧,我会说,这是由C#编译器,而不是CLR做的,但我明白你的意思。

  

如果我们试图重写它(不使用析构函数),会得到一个错误不覆盖object.Finalize,而是提供了一个析构函数。<​​/ P>

正确的。

  

似乎在mscorlib.dll Object类实现已敲定定义为保护覆盖无效的Finalize(){}

正确的。

  

那么我们为什么不能超越呢?这就是一个虚函数是。

由于然后会有的两个的方式来写一个析构函数。这将是混乱的。我们希望那里是写析构函数只有一个办法。

  

为什么这样设计?是它是用C ++析构函数的概念是一致的?

这是一个原因,是的。还有其他原因。这里有几个:

  • 通过逻辑分离析构函数的概念和覆盖finalize方法,我们有可能实现通过在其他环境中一些其他机制析构函数。这还没有发生,但它可能发生在未来,我们写一个版本的C#的,也就是说,是构建设备驱动程序在具有比标准CLR语义不同的垃圾收集器语义的环境。在这种环境中的析构函数的语义可能更喜欢那些C ++的析构函数而不是象GC终结。

  • 终结是的非常非常特殊的方法的。你不能叫他们像普通的方法;只有垃圾收集器可以给他们打电话。他们有不同的异常处理规则。他们必须确保基类的终结被称为严格派生下课终结。他们是如此特别,它似乎危险揭露他们像任何其他方法。如果你有一个方法,只是坐在那里,你可以打电话,你可以把你想成什么,等等,它可以很容易地忘记如何特殊的终结是。有一个特殊的语法强调,这是特殊的code。

  

此外,当我们去的对象类的定义,也没有提及的finalize方法,那么请问该怎么mscorlib.dll中定义显示的finalize函数。

假设,我们发现在一个名为MagicUnicorn对象浏览器的方法,如果你想打电话或覆盖它,你会得到一个错误说不这样做!。何苦显示MagicUnicorn方法在所有如果你不能用它做什么?这只是无用的噪音。

现在,如果你拆开mscorlib程序然后当然有真有特殊的Finalize方法。

  

这是否意味着默认的析构函数转换成最后确定的方法。

是的。

I am new to .net ..and i am confused with the destructor mechanism in C# ..please clarify

In C# destructors are converted to finalize method by CLR. If we try to override it (not using destructor ) , will get an error Error 2 Do not override object.Finalize. Instead, provide a destructor.

But it seems that the Object calss implementation in mscorlib.dll has finalize defined as protected override void Finalize(){} , then why cant we override it , that what virtual function for .

Why is the design like that , is it to be consistent with c++ destructor concept.

Also when we go to the definition of the object class , there is no mention of the finalize method , then how does the hmscorlib.dll definition shows the finalize function. Does it mean that the default destructor is converted to finalize method.

public class Object
{     

    public Object();
    public virtual bool Equals(object obj);        
    public static bool Equals(object objA, object objB);       
    public virtual int GetHashCode();       
    public Type GetType();      
    protected object MemberwiseClone();  
    public static bool ReferenceEquals(object objA, object objB);
    public virtual string ToString();
}

解决方案

am new to .NET and i am confused with the destructor mechanism in C#. Please clarify.

Sure. I agree that it is confusing.

In C# destructors are converted to finalize method by CLR.

Correct. Well, I'd say that this is done by the C# compiler, not the CLR, but I understand what you mean.

If we try to override it (not using destructor ) , will get an error "Do not override object.Finalize. Instead, provide a destructor."

Correct.

it seems that the Object class implementation in mscorlib.dll has finalize defined as protected override void Finalize(){}

Correct.

then why can't we override it? That's what a virtual function is for.

Because then there would be two ways to write a destructor. That would be confusing. We want there to be only one way to write a destructor.

Why is the design like that? is it to be consistent with c++ destructor concept?

That is one reason, yes. There are other reasons. Here are a few:

  • By logically separating the concepts of "destructor" and "override the finalize method" we make it possible to implement destructors via some other mechanism in other environments. This hasn't happened yet, but it could happen that in the future we write a version of C# that, say, is for building device drivers in an environment that has different garbage collector semantics than the standard CLR semantics. In that environment the semantics of the destructor might be more like those of the C++ destructor and less like a GC finalizer.

  • Finalizers are very very special methods. You cannot call them like regular methods; only the garbage collector can call them. They have different exception handling rules. They must ensure that base class finalizers are called strictly after derived class finalizers. They are so special that it seems dangerous to expose them as methods like any other. If you have a method just sitting there that you can call, that you can put anything you want into, and so on, it makes it easy to forget how special a finalizer is. Having a special syntax emphasizes that this is special code.

Also when we go to the definition of the object class, there is no mention of the finalize method , then how does the mscorlib.dll definition shows the finalize function .

Suppose we showed a method in the object browser called MagicUnicorn, and if you tried to call it or override it, you'd get an error saying "don't do that!". Why bother to show the MagicUnicorn method at all if you can't do anything with it? That's just unhelpful noise.

Now, if you disassemble mscorlib then of course there really is the special Finalize method there.

Does it mean that the default destructor is converted to finalize method.

Yes.

这篇关于为什么定案方法不能覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆