System.Object Class

Learn how every .NET type inherits from System.Object and how to override its virtual methods to customize object behavior.

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 a Type object 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.

First, let’s define a simple class to work with.