The Role of the System.Object’s Equals Instance Method

Defined on System.Object, the “mother type of all types,” the Equals instance method is used to compare whether two objects are “equal,” whatever that means for the types of the target object and the one being compared:

class Object{    ...    public virtual bool Equals(object obj) { /* default implementation */ }}

As you learn in Chapter 14, “Object-Oriented Programming,” virtual methods can be overridden by deriving types. It’s typical for types to do so for System.Object’s virtual methods, which include Equals, GetHashCode, and ToString. Before we get into overriding the Equals method, let’s discuss its default behavior.

For reference types, the default

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.