C# ReferenceEquals: When to Use It and How It Works
Learn how ReferenceEquals compares object identity in C#, how boxing affects value types, and when to use it instead of == or Equals.

ReferenceEquals is a static method on object that tells you whether two references point to the same instance. It never calls Equals and it never applies value equality. This article explains what the method checks, how boxing affects it, and when it is the right tool.
What ReferenceEquals Checks
In C#, object.ReferenceEquals determines whether two object references point to the same underlying object instance. It does not call any overridden Equals method and does not consider value equality. The method is defined as:
public static bool ReferenceEquals(object objA, object objB)Because the parameters are typed as object, any value type passed to this method is boxed before the comparison. This boxing behavior is a common source of confusion and subtle bugs.
ReferenceEquals vs == vs Equals
The == operator is overloadable, and its behavior is determined by the compile-time type of the operands. Equals is virtual, so types can override it to define value equality. ReferenceEquals is static and always compares identity.
For reference types, == typically compares references unless the type overloads the operator. string, for example, overloads == to compare string content rather than references. ReferenceEquals ignores such overloads.
Consider this example:
object a = new object();
object b = a;
Console.WriteLine(ReferenceEquals(a, b)); // True
string s1 = "hello";
string s2 = new string(s1.ToCharArray()); // creates a new string instance
Console.WriteLine(s1 == s2); // True (value equality)
Console.WriteLine(ReferenceEquals(s1, s2)); // False (different instances)The new string(s1.ToCharArray()) constructor call creates a separate string instance rather than reusing an interned literal. Without that, a second literal with the same text can be the same reference.
Value Types and Boxing
When you pass a non-null value type to ReferenceEquals, the value is boxed into a new object. For ordinary value types, two separate variables will never be reference-equal, even if they have identical values. For example:
int x = 5;
int y = 5;
Console.WriteLine(ReferenceEquals(x, y)); // FalseEach boxing operation for a non-null value creates a new object. Even if you box the same variable twice, you get two different references:
int x = 5;
object boxed1 = x;
object boxed2 = x;
Console.WriteLine(ReferenceEquals(boxed1, boxed2)); // FalseThere is no caching for general value types. The special case is Nullable<T>: a null nullable boxes to null, so ReferenceEquals(null, null) returns true. A non-null nullable still boxes normally.
Strings and Interning
The .NET runtime interns string literals. Two string literals with the same content therefore often reference the same interned instance. Strings created at runtime, however, are not guaranteed to be interned, so ReferenceEquals on strings is unreliable for equality checks.
string literal1 = "hello";
string literal2 = "hello";
Console.WriteLine(ReferenceEquals(literal1, literal2)); // True (interned)
string dynamic1 = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
string dynamic2 = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
Console.WriteLine(ReferenceEquals(dynamic1, dynamic2)); // False (usually)Whether two runtime-created strings share a reference is an implementation detail. Use string.Equals or == to compare string content.
When to Use ReferenceEquals
ReferenceEquals is useful when you need object identity rather than value equality. Common scenarios include:
- Caching or memoization: verifying that a cached object is the same instance.
- Event handlers: removing a handler by reference to ensure you remove the exact delegate instance.
- Testing frameworks: asserting that two objects are the same instance.
- Implementing custom
Equalsmethods: starting withReferenceEquals(this, obj)short-circuits identity checks.
For example:
public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj)) return true;
if (obj is null) return false;
if (GetType() != obj.GetType()) return false;
// Compare fields...
}This pattern avoids unnecessary field comparisons when the same instance is passed.
Performance and Runtime Cost
ReferenceEquals is effectively a reference comparison. It does not invoke virtual methods or type-specific logic, so its cost is minimal. Passing value types to it, however, incurs boxing, which allocates memory and copies the value. That overhead can matter in hot paths.
If you need to compare value types by value, do not use ReferenceEquals. Use EqualityComparer<T>.Default or, if identity is truly required, constrain a generic type to a reference type.
Common Mistakes and Edge Cases
A frequent mistake is assuming ReferenceEquals behaves like == for value types. It does not. Another is using it to compare strings for equality. Use string.Equals for content.
ReferenceEquals returns true when both arguments are null:
Console.WriteLine(ReferenceEquals(null, null)); // TrueIt is a static method on object, so it cannot be overridden.
ReferenceEquals can also be surprising in generic code. With a class constraint, reference identity works as expected:
public bool IsSame<T>(T a, T b) where T : class
{
return ReferenceEquals(a, b);
}Without the class constraint, value type arguments are boxed. For non-null value types, the two arguments are boxed separately and ReferenceEquals returns false. Only null arguments, which can occur with Nullable<T>, produce true. If a reference type ends up being used as T, normal reference semantics apply.
When ReferenceEquals Is Not Enough
ReferenceEquals is not a general-purpose equality check. It ignores overridden Equals methods and overloaded == operators, and it never compares values. If you need to know whether two objects have the same content or are semantically equal, use Equals, ==, or a specific comparison API instead.
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.