System.Object Class
Namespace
System
Assembly
mscorlib.dll
Inheritance
System.Object
Summary
The System.Object class is the ultimate base class of all .NET types. It provides methods that are essential for object manipulation, such as ToString, Equals, GetHashCode, and GetType.
Methods
| Method | Signature | Description |
|---|---|---|
ToString |
public virtual string ToString(); |
Returns a string that represents the current object. |
Equals |
public virtual bool Equals(object obj); |
Determines whether the specified object is equal to the current object. |
GetHashCode |
public virtual int GetHashCode(); |
Serves as the default hash function. |
GetType |
public Type GetType(); |
Gets the Type of the current instance. |
MemberwiseClone |
protected object MemberwiseClone(); |
Creates a shallow copy of the current object. |
Example
using System;
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public override string ToString() => $"{Name}, {Age} years old";
}
class Program
{
static void Main()
{
var p = new Person { Name = "Alice", Age = 30 };
Console.WriteLine(p.ToString()); // Output: Alice, 30 years old
// Using methods from System.Object
Console.WriteLine(p.GetType()); // System.Person
Console.WriteLine(p.Equals(p)); // True
Console.WriteLine(p.GetHashCode()); // hash code
}
}
Remarks
All .NET types derive, directly or indirectly, from System.Object. Overriding its virtual methods enables you to provide custom behavior for string representation, equality comparison, and hash code generation.