Object.ReferenceEquals(Object, Object) 方法


判斷指定的 Object 執行個體是否為相同的執行個體。Determines whether the specified Object instances are the same instance.

 static bool ReferenceEquals(System::Object ^ objA, System::Object ^ objB);
public static bool ReferenceEquals (object objA, object objB);
public static bool ReferenceEquals (object? objA, object? objB);
static member ReferenceEquals : obj * obj -> bool
Public Shared Function ReferenceEquals (objA As Object, objB As Object) As Boolean



要比較的第一個物件。The first object to compare.


要比較的第二個物件。The second object to compare.



如果 objAobjB 為相同的執行個體或兩者皆為 null,則為 true,否則為 falsetrue if objA is the same instance as objB or if both are null; otherwise, false.


下列範例會使用 ReferenceEquals 來判斷兩個物件是否為相同的實例。The following example uses ReferenceEquals to determine if two objects are the same instance.

using namespace System;
int main()
   Object^ o = nullptr;
   Object^ p = nullptr;
   Object^ q = gcnew 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.


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
Public Class App
    Public Shared Sub Main() 
        Dim o As Object = Nothing
        Dim p As Object = Nothing
        Dim q As New Object
        Console.WriteLine(Object.ReferenceEquals(o, p))
        p = q
        Console.WriteLine(Object.ReferenceEquals(p, q))
        Console.WriteLine(Object.ReferenceEquals(o, p))
    End Sub 
End Class 
' This code produces the following output:
' True
' True
' False


不同于 Equals 方法和等號比較運算子, ReferenceEquals 無法覆寫方法。Unlike the Equals method and the equality operator, the ReferenceEquals method cannot be overridden. 基於這個原因,如果您想要測試兩個物件參考是否相等,而且不確定方法的執行 Equals 方式,您可以呼叫 ReferenceEquals 方法。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.

不過, ReferenceEquals 在這兩個案例中,方法的傳回值可能會是異常:However, the return value of the ReferenceEquals method may appear to be anomalous in these two scenarios:

  • 在比較實值型別時。When comparing value types. 如果和是實值型別,則會在 objA objB 傳遞至方法之前先將它們裝箱 ReferenceEqualsIf objA and objB are value types, they are boxed before they are passed to the ReferenceEquals method. 這表示,如果 objA 和都 objB 代表相同的實值型別實例,則 ReferenceEquals 方法會傳回 false ,如下列範例所示。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.

    int int1 = 3;
    Console.WriteLine(Object.ReferenceEquals(int1, int1));
    // The example displays the following output:
    //       False
    //       True
    Public Module Example
       Public Sub Main
          Dim int1 As Integer = 3
          Console.WriteLine(Object.ReferenceEquals(int1, int1))
       End Sub
    End Module
    ' The example displays the following output:
    '       False
    '       True

    如需有關如何對實值型別進行裝箱的詳細資訊,請參閱裝箱和取消For information on boxing value types, see Boxing and Unboxing.

  • 比較字串時。When comparing strings. 如果 objAobjB 是字串,則 ReferenceEquals 方法 true 會在字串暫留時傳回。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. 在下列範例中, s1s2 是相等的,因為它們是單一留用字串的兩個實例。In the following example, s1 and s2 are equal because they are two instances of a single interned string. 不過, s3s4 不相等,因為雖然它們有相同的字串值,但該字串不會被暫留。However, s3 and s4 are not equal, because although they have identical string values, that string is not interned.

    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
    Module Example
       Public Sub Main()
          Dim s1 As String = "String1"
          Dim s2 As String = "String1"
          Console.WriteLine("s1 = s2: {0}", Object.ReferenceEquals(s1, s2))
          Console.WriteLine("{0} interned: {1}", s1, 
                            If(String.IsNullOrEmpty(String.IsInterned(s1)), "No", "Yes"))
          Dim suffix As String = "A"
          Dim s3 = "String" + suffix
          Dim s4 = "String" + suffix
          Console.WriteLine("s3 = s4: {0}", Object.ReferenceEquals(s3, s4))
          Console.WriteLine("{0} interned: {1}", s3, 
                            If(String.IsNullOrEmpty(String.IsInterned(s3)), "No", "Yes"))
       End Sub
    End Module
    ' The example displays the following output:
    '       s1 = s2: True
    '       String1 interned: Yes
    '       s3 = s4: False
    '       StringA interned: No

    如需有關字串暫留的詳細資訊,請參閱 String.IsInternedFor more information about string interning, see String.IsInterned.