Edit

Share via


Object.ReferenceEquals(Object, Object) Method

Definition

Determines whether the specified Object instances are the same instance.

C#
public static bool ReferenceEquals(object objA, object objB);
C#
public static bool ReferenceEquals(object? objA, object? objB);

Parameters

objA
Object

The first object to compare.

objB
Object

The second object to compare.

Returns

true if objA is the same instance as objB or if both are null; otherwise, false.

Examples

The following example uses ReferenceEquals to determine if two objects are the same instance.

C#
object o = null;
object p = null;
object q = new Object();

Console.WriteLine(Object.ReferenceEquals(o, p));
p = q;
Console.WriteLine(Object.ReferenceEquals(p, q));
Console.WriteLine(Object.ReferenceEquals(o, p));

// This code produces the following output:
//   True
//   True
//   False

Remarks

Unlike the Equals method and the equality operator, the ReferenceEquals method cannot be overridden. Because of this, if you want to test two object references for equality and you are unsure about the implementation of the Equals method, you can call the ReferenceEquals method.

However, the return value of the ReferenceEquals method may appear to be anomalous in these two scenarios:

  • When comparing value types. If objA and objB are value types, they are boxed before they are passed to the ReferenceEquals method. This means that if both objA and objB represent the same instance of a value type, the ReferenceEquals method nevertheless returns false, as the following example shows.

    C#
    int int1 = 3;
    Console.WriteLine(Object.ReferenceEquals(int1, int1));
    Console.WriteLine(int1.GetType().IsValueType);
    
    // The example displays the following output:
    //       False
    //       True
    

    For information on boxing value types, see Boxing and Unboxing.

  • When comparing strings. If objA and objB are strings, the ReferenceEquals method returns true if the string is interned. It does not perform a test for value equality. In the following example, s1 and s2 are equal because they are two instances of a single interned string. However, s3 and s4 are not equal, because although they have identical string values, that string is not interned.

    C#
    String s1 = "String1";
    String s2 = "String1";
    Console.WriteLine("s1 = s2: {0}", Object.ReferenceEquals(s1, s2));
    Console.WriteLine("{0} interned: {1}", s1,
                      String.IsNullOrEmpty(String.IsInterned(s1)) ? "No" : "Yes");
    
    String suffix = "A";
    String s3 = "String" + suffix;
    String s4 = "String" + suffix;
    Console.WriteLine("s3 = s4: {0}", Object.ReferenceEquals(s3, s4));
    Console.WriteLine("{0} interned: {1}", s3,
                      String.IsNullOrEmpty(String.IsInterned(s3)) ? "No" : "Yes");
    
    // The example displays the following output:
    //       s1 = s2: True
    //       String1 interned: Yes
    //       s3 = s4: False
    //       StringA interned: No
    

    For more information about string interning, see String.IsInterned.

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

See also