Object.ReferenceEquals(Object, Object) 方法

定義

判斷指定的 Object 執行個體是否為相同的執行個體。

public:
 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

參數

objA
Object

要比較的第一個物件。

objB
Object

要比較的第二個物件。

傳回

Boolean

如果 objAobjB 為相同的執行個體或兩者皆為 null,則為 true,否則為 false

範例

下列範例會使用 ReferenceEquals 來判斷兩個物件是否為相同的實例。

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.

True
True
False

*/
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
let o: obj = null
let mutable p: obj = null
let q = obj ()

printfn $"{Object.ReferenceEquals(o, p)}"
p <- q
printfn $"{Object.ReferenceEquals(p, q)}"
printfn $"{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 無法覆寫 方法。 因此,如果您想要測試兩個物件參考是否相等,而且不確定方法的實作 Equals ,您可以呼叫 ReferenceEquals 方法。

不過,在下列兩個案例中,方法的 ReferenceEquals 傳回值可能會異常:

  • 比較實值型別時。 如果 objAobjB 是實值型別,則會先進行 Box 處理,再將它們傳遞至 ReferenceEquals 方法。 這表示,如果 和 objBobjA 代表實值型別的相同實例, ReferenceEquals 方法仍會傳 false 回 ,如下列範例所示。

    int int1 = 3;
    Console.WriteLine(Object.ReferenceEquals(int1, int1));
    Console.WriteLine(int1.GetType().IsValueType);
    
    // The example displays the following output:
    //       False
    //       True
    
    let int1 = 3
    printfn $"{Object.ReferenceEquals(int1, int1)}"
    printfn $"{int1.GetType().IsValueType}"
    
    // 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))
          Console.WriteLine(int1.GetType().IsValueType)
       End Sub
    End Module
    ' The example displays the following output:
    '       False
    '       True
    

    如需 Boxing 實值型別的資訊,請參閱 Boxing 和 Unboxing

  • 比較字串時。 如果 objAobjB 是字串,則如果字串為terned,則 ReferenceEquals 方法會傳回 true 。 它不會針對值相等執行測試。 在下列範例中, s1s2 相等,因為它們是單一三元字串的兩個實例。 不過, s3s4 不相等,因為雖然它們具有相同的字串值,但該字串不是三元。

    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
    
    open System
    
    let s1 = "String1"
    let s2 = "String1"
    printfn $"s1 = s2: {Object.ReferenceEquals(s1, s2)}"
    printfn $"""{s1} interned: {if String.IsNullOrEmpty(String.IsInterned s1) then "No" else "Yes"}"""
    
    let suffix = "A"
    let s3 = "String" + suffix
    let s4 = "String" + suffix
    printfn $"s3 = s4: {Object.ReferenceEquals(s3, s4)}"
    printfn $"""{s3} interned: {if String.IsNullOrEmpty(String.IsInterned s3) then "No" else "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.IsInterned

適用於

另請參閱