![]() |
VOOZH | about |
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.
Example:
Output:
For Object obj1 = new Object(); Object System.Object System For String str System.ValueType Int32 System.Int32 System
| 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. |
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: