4 out of 6 rated this helpful - Rate this topic

Object.ReferenceEquals Method

Determines whether the specified Object instances are the same instance.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static bool ReferenceEquals(
	Object objA,
	Object objB
)

Parameters

objA
Type: System.Object

The first object to compare.

objB
Type: System.Object

The second object to compare.

Return Value

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

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.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          int int1 = 3;
          Console.WriteLine(Object.ReferenceEquals(int1, int1));
          Console.WriteLine(int1.GetType().IsValueType);
       }
    }
    // The example displays the following output: 
    //       False 
    //       True
    
  • 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 are have identical string values, that string is not interned.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          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("s1 = s2: {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 
    //       s1 = s2: False 
    //       StringA interned: No
    

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

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

using System;

class MyClass {

   static void Main() {
      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

*/

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.