System.Object Class
Explore the System.Object class, the ultimate base for all .NET types. Understand the default methods it provides, like ToString, Equals, GetHashCode, and GetType. Learn how to override these methods to customize object behavior, ensuring correct equality checks and hash codes for collections.
We'll cover the following...
All .NET types, including custom ones, inherit from the System.Object class. This inheritance occurs implicitly even if we do not declare it. As the ultimate base class in the hierarchy, System.Object defines the fundamental behavior available to all types.
Note: The object keyword is an alias for System.Object.
Methods of System.Object
The object class defines four primary methods available to every type in .NET:
ToString(): Returns a string representation of the object.GetHashCode(): Returns a numeric hash code used in hash-based collections.GetType(): Returns aTypeobject containing metadata about the instance.Equals(): Compares two objects for equality. The default for reference types is reference equality.
With the exception of GetType(), these methods are virtual. This allows us to override them if their default implementation does not meet our needs. ...