C# | Object Class

The Object class is the base class for all the classes in .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 Object class and if extends other class then it is an 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 the 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. does 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 of such wrapping due to the presence of value types which are inherited from System.ValueType that are further inherited from System.Object. So in C#, value types also work similar 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 value type that implicitly inheriting the Object class through the System.ValueType type.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
        Console.WriteLine(t1.BaseType);
  
        // It will return the name class
        Console.WriteLine(t1.Name);
  
        // It will return the
        // fully qualified name
        Console.WriteLine(t1.FullName);
  
        // It will return the Namespace
        // By default Namespace is System
        Console.WriteLine(t1.Namespace);
  
        Console.WriteLine();
  
        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
        Console.WriteLine(t2.BaseType);
  
        // It will return the name class
        Console.WriteLine(t2.Name);
  
        // It will return the
        // fully qualified name
        Console.WriteLine(t2.FullName);
  
        // It will return the Namespace
        // By default Namespace is System
        Console.WriteLine(t2.Namespace);
    }
}

chevron_right


Ouput:

For Object obj1 = new Object();

Object
System.Object
System

For String str
System.ValueType
Int32
System.Int32
System

Constructor

Constructor Description
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.

Methods

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

Methods Description
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 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 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.


My Personal Notes arrow_drop_up

Believes in Smart Work

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.