.NET FAQs Unleashed!

 
 


    



    



Exploring .NET System.Object - The Inside Story

System.Object is the Adam & Eve of all classes in .NET. In .NET, every class is derived from System.Object. The Object class comprises of a set of members that is there in every class in .NET. By default, if a class is created and its parent type is not specified, it inherits from System.Object.

CustomClass

{

// The CustomClass defined here automatically inherits from System.Object as

// the parent class, because no parent class has been explicitly specified

}

The System.Object comprises a set of virtual members (that may be overriden) and static members. Checkout the code construct of the Object class.

namespace System

{

  public Object();

  public virtual Boolean Equals(Object obj);

  public virtual Int32 GetHashCode();

  public Type GetType();

  public virtual String ToString();

  protected virtual void Finalize();

  protected Object MemberwiseClone();

  public static bool Equals(object objA, object objB);

  public static bool ReferenceEquals(object objA, object objB);

}

Note here that all members have a public specifier except the MemberwiseClone() & the Finalize() method. Following is a description of the pivot members of the System.Object class:

  1. Equals() - This method is used to compare the references of objects. Usually, this method is overriden, and it returns true when the compared objects possess similar internal values.
  2. GetHashCode() - This method returns an integer, that is specific to an object in the memory.
  3. GetType() - This method returns the type of the object at runtime.
  4. ToString() - The object in context may be converted to its string format using this method.
  5. Finalize() - This is a protected method, so it is basically handled by the .NET Runtime Engine. It is responsible for removing an object from the memory heap.
  6. MemberwiseClone() - This is also a protected method. It may be utilized to create an exact copy of the object in context. This copy comprises of same members as of the master object. When an object has references to other objects in the memory, shallow copies are created.

Now lets have an understanding how the Equals() method is used. Look at the following code:

namespace ObjectNamespace

{

  class Lawyers

  {

    //Constructor for this class goes here

    public Lawyers(string sName, string sExpertise)

    {

      Name = sName;

      Expertise = sExpertise; 

    }

    public Lawyers() {}

    public string Name;

    public string Expertise;

  }

}  

// Following code may be used in the Main() method

static void Main(string[] args)

{

  Console.Writeline("~~~Checkout behavior of Equals()~~~");

  Lawyers law = new Lawyers("Vishal","Real Estate");

  // Now checkout usage of ToString() and GetHashCode() methods here

  Console.WriteLine("ToString() : {0}", law.ToString());

  Console.WriteLine("GetHashCode() : {0}", law.GetHashCode());

  //The line below shall display base class of law

  Console.WriteLine("Base Class : {0}", law.GetType().BaseType());

  // Now checkout usage of the Equals() method

  Lawyers law2 = law; // Reference to existing object made

  object obj = law2;

  // Display where is the reference being made

  if(obj.Equals(law) && law2.Equals(obj))

   Console.Writeline("All 3 objects point to the same place in memory");

}

Note that if required, the members inside the System.Object may also be overriden. The members that are defined using the virtual keyword may be overriden using the override keyword.

The System.Object class also contains two extremely useful Static members.

  1. Object.Equals() - Used to check value based equality.
  2. Object.ReferenceEquals() - Used to check reference based equality.

So if two objects law1 and law2 are declared:

Lawyers law1 = new Lawyers("Tom","Criminal Lawyer");

Lawyers law2 = new Lawyers("Jerry","Real Estate Lawyer");

Console.Writeline("law1 and law2 have same state : {0}",object.Equals(law1, law2));

Console.Writeline("law1 and law2 reference to same object : {0}",object.ReferenceEquals(law1, law2));

So thats pretty simple. The Equals() method states whether the objects being compared are of the same type. ReferenceEquals() is used to figure out whether the objects being compared point to the same object in the memory heap.

Happy Programming.

Cheers!