Drill Down into System.Object

Author: Gopinath Devadass   8. November 2009


The CLR requires each and every object must be derived from System.Object class. Also each and every class in .NET must have few set of methods.

I have created a class in 2 different ways, but both are same for CLR at the run time.

Class Student {

}

Class Student : System.Object {

}

System.Object has the following Public methods:

·         GetType - it returns the type of the current object. By using this method we can get all the metadata for the current object type. It’s a nonvirtual method, so we can’t override this method.

 

 

 

·         Equals – It returns true when the two objects have the same value.  We can override this method in our class if required.

Student _student = new Student();

_student.Name = "Rahul";

object objStudent = _student;

 

//Status of the object is true

bool objectStatus = _student.Equals(objStudent);

·         ToString - It returns the full name of the current object type (obj.GetType ().FullName). Also we can override this method in our class.

 

·         GetHashCode – This method return a value of the object in current instance. By default it returns a new value when we execute the application. But we can override this method to return a static value. This method is used to specify the keys in has table.

 

System.Object has the following Protected methods:

·         Finalize - This method id used by the Garbage Collector to clean the object.

 

·         MemberwiseClone - It returns a new instance of the current object. When this method is invoked, it creates a new instance and set the default instance value to its fields.

.NET