Open In App
Related Articles

C# | Object Class

Like Article
Save Article
Report issue

The Object class is the base class for all the classes in the .Net Framework. It is present in the System namespace. In C#, the .NET Base Class Library(BCL) has a language-specific alias which is Object class with the fully qualified name as System.Object. Every class in C# is directly or indirectly derived from the Object class. If a class does not extend any other class then it is the direct child class of the Object class and if extends another class then it is indirectly derived. Therefore the Object class methods are available to all C# classes. Hence Object class acts as a root of the inheritance hierarchy in any C# Program. The main purpose of the Object class is to provide low-level services to derived classes. 

There are two types in C# i.e Reference types and Value types. By using System.ValueType class, the value types inherit the object class implicitly. System.ValueType class overrides the virtual methods from Object Class with more appropriate implementations for value types. In other programming languages, the built-in types like int, double, float, etc. do not have any object-oriented properties. To simulate the object-oriented behavior for built-in types, they must be explicitly wrapped into the objects. But in C#, we have no need for such wrapping due to the presence of value types that are inherited from the System.ValueType that is further inherited from System.Object. So in C#, value types also work similarly to reference types. Reference types directly or indirectly inherit the object class by using other reference types.


Explanation of the above Figure: Here, you can see the Object class at the top of the type hierarchy. Class 1 and Class 2 are the reference types. Class 1 is directly inheriting the Object class while Class 2 is Indirectly inheriting by using Class 1. Struct1 is a value type that implicitly inherits the Object class through the System.ValueType type. 



// C# Program to demonstrate
// the Object class
using System;
using System.Text;
class Geeks {
    // Main Method
    static void Main(string[] args)
        // taking object type
        Object obj1 = new Object();
        // taking integer
        int i = 10;
        // taking Type type and assigning
        // the value as type of above
        // defined types using GetType
        // method
        Type t1 = obj1.GetType();
        Type t2 = i.GetType();
        // Displaying result
        Console.WriteLine("For Object obj1 = new Object();");
        // BaseType is used to display
        // the base class of current type
        // it will return nothing as Object
        // class is on top of hierarchy
        // It will return the name class
        // It will return the
        // fully qualified name
        // It will return the Namespace
        // By default Namespace is System
        Console.WriteLine("For String str");
        // BaseType is used to display
        // the base class of current type
        // it will return System.Object
        // as Object class is on top
        // of hierarchy
        // It will return the name class
        // It will return the
        // fully qualified name
        // It will return the Namespace
        // By default Namespace is System



For Object obj1 = new Object();


For String str


.object-table { border-collapse: collapse; width: 100%; } .object-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .object-table th { border: 1px solid #5fb962; padding: 8px; } .object-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .object-table tr:nth-child(odd) { background-color: #ffffff; } 
Object()Initializes a new instance of the Object class. This constructor is called by constructors in derived classes, but it can also be used to directly create an instance of the Object class.


There are total 8 methods present in the C# Object class as follows:

Equals(Object)Determines whether the specified object is equal to the current object.
Equals(Object, Object)Determines whether the specified object instances are considered equal.
Finalize()Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
GetHashCode()Serves as the default hash function.
GetType()Gets the Type of the current instance.
MemberwiseClone()Creates a shallow copy of the current Object.
ReferenceEquals(Object, Object)Determines whether the specified Object instances are the same instance.
ToString()Returns a string that represents the current object.

Important Points:

  • C# classes don’t require to declare the inheritance from the Object class as the inheritance is implicit.
  • Every method defined in the Object class is available in all objects in the system as all classes in the .NET Framework are derived from the Object class.
  • Derived classes can and do override Equals, Finalize, GetHashCode, and ToString methods of Object class.
  • The process of boxing and unboxing a type internally causes a performance cost. Using the type-specific class to handle the frequently used types can improve the performance cost.

Last Updated : 08 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads