C# Object.Equals vs ReferenceEquals: Key Differences
Object.Equals and ReferenceEquals answer different questions in C#: value equality vs reference identity. Learn how each works, when to override Equals, and common pitfalls.

In C#, the virtual instance method Object.Equals(object) and the static method ReferenceEquals answer different questions. ReferenceEquals checks whether two references point to the same instance. Object.Equals can be overridden to define value equality, where separate objects with the same data compare as equal. The distinction matters when you work with custom types, collections, or equality-based operations like HashSet<T> or Dictionary<TKey, TValue>.
Default Object.Equals Behavior
For reference types, the default implementation of Object.Equals(object) is reference equality: it returns true only when both references point to the same object. In that case it behaves like ReferenceEquals. For value types, the default implementation inherited from System.ValueType uses reflection to compare all fields, which is slower; many built-in value types override it with a faster implementation.
public class Person
{
public string Name { get; set; }
}
var p1 = new Person { Name = "Alice" };
var p2 = new Person { Name = "Alice" };
Console.WriteLine(p1.Equals(p2)); // False
Console.WriteLine(ReferenceEquals(p1, p2)); // FalseBoth calls return false because p1 and p2 are separate instances. The default Equals for a reference type does not compare field values.
ReferenceEquals Behavior
ReferenceEquals is a static method on Object. It never calls a virtual method; it checks whether two references point to the same object. When you pass non-null value types, they are boxed before being passed as object, and each boxing conversion creates a new boxed instance, so ReferenceEquals returns false.
int x = 42;
int y = 42;
Console.WriteLine(ReferenceEquals(x, y)); // FalseEven though x and y have the same value, they are boxed into separate objects, so reference equality fails.
When Overriding Equals Changes Behavior
If you override Equals in a class, you define what it means for two objects to be equal. This is common when you want value semantics: two separate instances with the same data should be considered equal. The override must be consistent with GetHashCode so hash-based collections work correctly.
public class Person
{
public string Name { get; set; }
public override bool Equals(object obj)
{
if (obj is Person other)
{
return Name == other.Name;
}
return false;
}
public override int GetHashCode()
{
return Name?.GetHashCode() ?? 0;
}
}With this override, p1.Equals(p2) returns true when both Person instances have the same Name. However, ReferenceEquals(p1, p2) still returns false, because overriding Equals does not change reference identity.
Value Equality vs. Reference Equality in Practice
The right choice depends on the meaning of the type. A type that represents an entity with a stable identity, such as a database record, may be better off with the default reference behavior. A value object, such as Money or Coordinate, should implement value equality so separate instances with the same data compare as equal.
public class Coordinate
{
public int X { get; }
public int Y { get; }
public Coordinate(int x, int y)
{
X = x;
Y = y;
}
public override bool Equals(object obj) =>
obj is Coordinate other && X == other.X && Y == other.Y;
public override int GetHashCode()
{
unchecked
{
return (X * 397) ^ Y;
}
}
}Now new Coordinate(1, 2).Equals(new Coordinate(1, 2)) returns true, while ReferenceEquals(new Coordinate(1, 2), new Coordinate(1, 2)) returns false. This value-based behavior keeps duplicate coordinates out of a HashSet<Coordinate>.
Common Pitfalls with Equals and ReferenceEquals
Do not assume == behaves like Equals. For reference types, == uses reference equality unless the operator is overloaded. For a custom value type, == is unavailable unless the type overloads it. Another common mistake is overriding Equals without also overriding GetHashCode; that breaks dictionary lookups and hash sets.
ReferenceEquals is rarely useful with value types. When you assign a value type to an object variable, the value is boxed, and separate assignments create separate boxed objects:
int a = 10;
object boxedA = a;
object boxedB = a;
Console.WriteLine(ReferenceEquals(boxedA, boxedB)); // FalseEach boxing operation creates a new object, so the two boxed references are not identical.
Performance and Allocation Considerations
ReferenceEquals is a direct reference comparison and does not involve virtual dispatch, so it is fast. Object.Equals may involve a virtual call, and an override can perform field comparisons that are more expensive. For value types, the default ValueType.Equals implementation uses reflection, which is slow; many built-in value types provide optimized overrides.
Before adding a custom Equals, think about whether reference equality is sufficient for the type. Field comparisons are usually cheap, but comparing a type that contains large collections can be expensive. In high-performance code, ReferenceEquals can be useful as an identity-only check, but it does not provide value semantics.
Choosing the Right Comparison for Your Type
ReferenceEquals is not a replacement for Object.Equals; the two serve different purposes. Use ReferenceEquals when you need to know whether two variables point to the same instance, such as in a cache or while tracking object lifetime. Use Object.Equals and its overrides when you want value equality in collections and LINQ operations.
If a type should have value semantics, override both Equals and GetHashCode. If the type represents an entity with identity and the default reference behavior is intentional, leave Equals untouched. That keeps the behavior predictable and avoids surprising cases where two separate instances are considered equal.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.