C# Object Class

In C# System.Object class is the base class of all the classes defined in .NET. It does not matter that the class is a user defined class or a system defined class.

Basically, when a user defined class created, the compiler automatically assumes it derives from System.Object class.

In practical, While using a class and object of it, we get some extra useful methods, despite that, we have never defined them in our class.

These methods reside under System.Object class which is automatically served by .NET for all the class that we define. If you will notice in the following image, the IntelliSense is showing some extra methods which are not defined in the Customer class.

Following is the list of some important methods of System.Object class and their uses.

Equals(Object)

Equals method of C# Object is used to check the equality of two objects.

Example

Try It
public static void Main()
{
    Customer customer = new Customer();
    customer.FullName = "Mark";
 
    Customer customer1 = customer;
    Console.WriteLine(customer.Equals(customer1));
 
    Customer customer2 = new Customer();
    Console.WriteLine(customer.Equals(customer2));
}
 
public class Customer
{
    string Name = string.Empty;
    public string FullName { get; set; }
}
 
//Output
//True
//False

Equals(Object,Object)

The Equals(Object,Object) is a static method of Object Class. As this is a static method, this is invoked by its class name. This method is little different than the Equals(object) method. Equals(Object,Object) is used to determine that the objects specified in this method are equal or not.

Object.GetType()

The Object.GetType() Method reurns the System.Type of current instance. Let's understand this method using an example.

Example

Try It
public static void Main()
{
    int a = 10;
    int b = 10;
 
    string name = "Mark";
 
    Customer customer = new Customer();
    customer.FullName = "Mark";
 
    Console.WriteLine("The type of customer object is : " + customer.GetType());
 
    Console.WriteLine("The type of [a] is : " + a.GetType());
 
    Console.WriteLine("The type of [name] is : " + name.GetType());
 
    Console.WriteLine("a and b are the same type: {0}",
                    Object.ReferenceEquals(a.GetType(), b.GetType()));
}
Output

The type of customer object is : Customer
The type of [a] is : System.Int32
The type of [name] is : System.String
a and b are the same type: True

In the above example, there are two integers and a string class field, the underlying Type of a and b is System.Int32 and the underlying type of name class field is System.String. We have also created an object of Customer class. Here the type of customer object is Customer class.

As you have learned above that System.Object class is the base class of all the classes defined in .NET. So the GetType method of Object Class can be used to get the type of all objects who belongs to the .NET framework.

ToString()

The ToString() is used to get the string representation of an object, To get something meaningful out of the string representation of an object, the ToString() method can be overridden. ToString() does not handle the null objects and it throws a null reference exception if the object is null. On a null object, ToString() method can't be called. Let's understand this with an example.

Example

Try It
public static void Main()
{
    Customer customer = new Customer();
    customer.FullName = "Mark";
 
    Console.WriteLine(customer.ToString());
}
//Output
//Customer

Following throws a null reference exception if ToString() used on a null object.

Customer customer = null;
Console.WriteLine(customer.ToString()); //[System.NullReferenceException: Object reference not set to an instance of an object.]?

The ToString() is also used to format the date in various forms.

In this article, only main methods of C# Object Class have been presented. If you want to learn more about the Object class please refer to MSDN.

Let's learn about the extension methods in the next section.