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.
For Object obj1 = new Object(); Object System.Object System For String str System.ValueType Int32 System.Int32 System
|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.|
- 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.
- C# | Class and Object
- C# | Check if an array object is equal to another array object
- C# | Add an object to the end of the ArrayList
- C# | Add an object to the end of Collection<T>
- Object and Dynamic Array in C#
- Object.ReferenceEquals() Method in C#
- C# | Method returning an object
- C# | Getting the key at the specified index of a SortedList object
- C# | Getting the keys in a SortedList object
- C# | Getting the Values in a SortedList object
- C# | Get an IDictionaryEnumerator object in OrderedDictionary
- Object and Collection Initializer in C#
- C# | Search in a SortedList object
- C# | Getting the value at the specified index of a SortedList object
- Different ways to create an Object in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.