Summary: in this tutorial, you’ll learn about the C# Object class and how to override the methods of the Object
class including ToString()
, Equals()
, and GetHashCode()
.
Introduction to the C# object class
All types in .NET implicitly inherit from the System.Object
class. In other words, the System.Object
is the base class of all classes. The System.Object
class provides low-level functionalities to the subclasses.
Note that the object is a synonym for the System.Object
class. Therefore, they refer to the same object.
When you define a class, it is implicitly inherited from the System.Object
class. And you don’t need to explicitly specify it.
The following example defines the Person
class:
class Person
{
public string SSN { get; set; }
public string Name { get; set; }
public byte Age { get; set; }
public Person(string ssn, string name, byte age)
{
SSN = ssn;
Name = name;
Age = age;
}
}
Code language: C# (cs)
In this example, the Person
class implicitly inherits from the System.Object
class. It can reuse or customize the methods in the Object
class.
For example, the GetType()
method returns the type of an instance of a class:
// Program.cs
var p1 = new Person("545–57-1111", "John Doe", 25);
Console.WriteLine(p1.GetType());
Code language: C# (cs)
Output:
Person
Code language: C# (cs)
In this example, we call the GetType()
method on an instance of the Person
class. Therefore, its type is Person
.
ToString()
The ToString()
method returns the string representation of an object. By default, the ToString()
returns the type of the object. For example:
// Program.cs
var p1 = new Person("545–57-1111", "John Doe", 25);
Console.WriteLine(p1.ToString());
Code language: C# (cs)
Output:
Person
Code language: C# (cs)
Note that you don’t need to call the ToString()
method explicitly:
// Program.cs
var p1 = new Person("545–57-1111", "John Doe", 25);
Console.WriteLine(p1);
Code language: C# (cs)
To customize the string representation of an object, you can override the ToString()
method of the System.Object
the class. For example:
class Person
{
public string SSN { get; set; }
public string Name { get; set; }
public byte Age { get; set; }
public Person(string ssn, string name, byte age)
{
SSN = ssn;
Name = name;
Age = age;
}
public override string ToString()
{
return $"Person(ssn={SSN},name={Name},age={Age})";
}
}
Code language: C# (cs)
In this example, we override the ToString()
method of the System.Object
class to return a string representation of the Person
‘s objects.
// Program.cs
var p1 = new Person("545–57-1111", "John Doe", 25);
Console.WriteLine(p1);
Code language: C# (cs)
Output:
Person(ssn=545-57-1111,name=John Doe,age=25)
Code language: C# (cs)
Equals()
To compare two objects, you use the Equals
method of the System.Object
class.
Typically, you need to override the Equals
method in your class to specify the logic to determine if two objects are equal. For example:
class Person
{
public string SSN { get; set; }
public string Name { get; set; }
public byte Age { get; set; }
public Person(string ssn, string name, byte age)
{
SSN = ssn;
Name = name;
Age = age;
}
public override string ToString()
{
return $"Person(ssn={SSN},name={Name},age={Age})";
}
public override bool Equals(object? obj)
{
if (obj == null || !GetType().Equals(obj.GetType()))
{
return false;
}
var other = (Person)obj;
return SSN == other.SSN;
}
}
Code language: C# (cs)
In this example, we override the Equals
method of the System.Object
class in the Person
class.
If the object is null
or its type is not the same, the Equals
method returns false
. Otherwise, it returns true
if the social security number (SSN) of two people are the same.
// Program.cs
var p1 = new Person("545–57-1111", "Jane Doe", 22);
var p2 = new Person("545–57-1111", "Jane Smith", 23);
Console.WriteLine(p1.Equals(p2));
Code language: C# (cs)
Output:
True
Code language: C# (cs)
GetHashCode()
The GetHashCode()
method returns a hash code for the current object. C# uses the hash code returned by the GetHashCode()
method to check the object equality.
If two objects are equal, the GetHashCode()
method returns the same hash code. However, the reverse is not true. The equal hash codes do not mean object equality because unequal objects can have the same hash codes.
The following shows how to override the GetHashCode()
method:
class Person
{
public string SSN { get; set; }
public string Name { get; set; }
public byte Age { get; set; }
public Person(string ssn, string name, byte age)
{
SSN = ssn;
Name = name;
Age = age;
}
public override string ToString()
{
return $"Person(ssn={SSN},name={Name},age={Age})";
}
public override bool Equals(object? obj)
{
if (obj == null || !GetType().Equals(obj.GetType()))
{
return false;
}
var other = (Person)obj;
return SSN.Equals(other.SSN);
}
public override int GetHashCode()
{
return SSN.GetHashCode();
}
}
Code language: C# (cs)
Summary
- The
System.Object
is the base class of all classes in .NET - The
object
is the alias of theSystem.Object
class. - Override the
ToString()
to customize the string representation of the object. - Override the
Equals()
method to determine if two objects are equal. - Override the
GetHashCode()
method to return a hash code for the object