Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finalize method in System.Object class

Out of curiosity i disassembled mscorlib.dll to check the implementation of System.Object class.

I found something weird in that.

1).    
public class Object {
...
    protected override void Finalize(){}
...
}

How come a base class has an overriden method in it?

2) public class Employee {
            public void InstanceMethod() {
                this.Finalize();
                //Does not compile, can i not access protected methods of base class??
            }
        }

I am just wondering what's the use of "protected Finalize" method in Object class and why it has got special treatment by compiler?

like image 588
Manish Basantani Avatar asked Jun 17 '26 13:06

Manish Basantani


1 Answers

It is a bug in Reflector, it gets confused by a method that's virtual but doesn't have the "newslot" attribute and doesn't have a base class type. It might be easier to see when you switch the decompiler to IL.

The real declaration of the finalizer, as copied from the Reference Source, is much as you'd expect it to be:

// Allow an object to free resources before the object is reclaimed by the GC.
//
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
~Object()
{
}
like image 153
Hans Passant Avatar answered Jun 19 '26 04:06

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!