Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Finalize method not allowed to override

Tags:

c#

.net

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();
}
like image 838
somaraj Avatar asked Apr 06 '10 17:04

somaraj


2 Answers

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.

like image 155
Eric Lippert Avatar answered Sep 30 '22 19:09

Eric Lippert


'Why is it so?'
By a design decision. Probably to match up with C++ but it really doesn't matter.

how does the hmscorlib.dll definition shows the finalize function

There probably is a special intervention in the C# compiler. Again, it makes no practical difference.

If you really need a finalizer, write a destructor. More important: you almost never need to.

To see how arbitrary this choice is: in VB you can override Finalize. And those two languages can quite happily use (and inherit) each others types.

like image 42
Henk Holterman Avatar answered Sep 30 '22 17:09

Henk Holterman