a blog for those who code

Saturday, 2 August 2014

Dot Net : All types are derived from System.Object

In this post we will expand on the statement "All types are derived from System.Object". CLR requires every type to ultimately be derived from the System.Object type.

This means...

class A {

is identical to

class A : System.Object {

Since its mandatory for every type to be ultimately derived from System.Object, every type has a minimum set of methods. They are

1. Equal - Returns true if two objects have the same value.
2. GetHashCode - Returns a hash code for this object's value.
3. ToString - Returns the full name of the type.
4. GetType - Returns an instance of a Type-derived object that identifies the type of the object used to call GetType.

The above methods are public methods of System.Object. The protected methods of System.Object are

1. MemberwiseClone - This creates a new instance of the type and sets the new object's instance fields to be identical to the this object's instance fields. A reference to the new instance is returned.

2. Finalize - This virtual method is called when the garbage collector determines that the object is garbage and before the memory for the object is reclaimed.

One more thing to know that CLR requires all objects to be created using the new operator. It has following functions :

1. It calculates the number of bytes required by all instance fields defined in the type and all of its base types up to and including System.Object. The bytes for these additional members are added to the size of the object.

2. It allocates memory for the object by allocating the number of bytes required for the specified type from the managed heap; all of these bytes are then set to zero (0).

3. It initializes the object's type object pointer and sync block index members.

4. The type's instance constructor is called, passing it any arguments specified in the call to new.

After performing all of these operations, it returns a reference to the newly created object. The new operator doesn't have delete operator, the object created will be removed by garbage collector when it detects that the object is no longer being used.

Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment

Contact Us


Email *

Message *


Best Hosting Plans

CC License