All classes in the .NET Framework inherits from the System.Object class. The System.Object class is mapped to the object keyword in C#. For simplicity, I will just use object to refer to System.Object. The following is a list of some common methods of the objectclass.

Method Return Type Virtual Static Description
Object() None No No This is the constructor of
the System.Object class.
~Object() None No No This is the destructor of
the System.Object class.
Equals(object) bool Yess No Accepts another object as the
argument and returns true if
the object refers to the same
object that called this method.
Equals(object, object) bool No Yes A static method that accepts
two objects and determines
if they are referring to the same
object.
ReferenceEquals
(object,object)
bool No Yes A static method that determines
if the two objects are the
same instance.
ToString() string Yes No Returns a string representation
of the object. You can override
this method to provide your
own string representation
for your class.
MemberwiseClone() object Noo No This method creates a new instance
of the object and copies the
values of its data members.
GetType() System.Type No No Returns the type of the
object as System.Type
GetHashCode() int Yes No Returns a hash value which
identifies the object state.

Figure 1 – System.Object Members

Not all of this methods are used frequently. Since all classes in C# inherits from this class, they also possess these methods except for the static methods. When you are creating a class, the class implicitly inherits from the object class. So when you are declaring a class, this is the actual code read by the compiler.

class MyClass : System.Object
{
}

The purpose of why every class in .NET inherits from object is to take advantage of polymorphism which will be discussed later. For example, one of the overloads of the Console.WriteLine() method accepts an object argument. That’s why you can pass almost anything as an argument to the Console.WriteLine() method. To demonstrate that anything is an object in C#, let’s take a look at a sample program that follows.

using System;
 
namespace ObjectDemo
{
    public class Program
    {
        public static void Main()
        {
            int myInt = 1;
            double myDouble = 4.0;
            string myString = "Hello";
 
            Console.WriteLine(myInt.ToString());
            Console.WriteLine(myDouble.ToString());
            Console.WriteLine(myString.ToString());
        }
    }
}

Example 1 – Everything Inherits from System.Object Class

 You can see that intdouble, and string objects all called the ToString() method which is a method inherited from the objectclass.