Why Finalize method not allowed to override

Posted by somaraj on Stack Overflow See other posts from Stack Overflow or by somaraj
Published on 2010-04-06T17:42:09Z Indexed on 2010/04/06 17:43 UTC
Read the original article Hit count: 413

Filed under:

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 funtion . 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();
}

© Stack Overflow or respective owner

Related posts about .NET