Object.ReferenceEquals(Object, Object) Metodo

Definizione

Determina se le istanze specificate Object sono la stessa istanza.

public:
 static bool ReferenceEquals(System::Object ^ objA, System::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

Parametri

objA
Object

Primo oggetto da confrontare.

objB
Object

Secondo oggetto da confrontare.

Valori restituiti

true se objA è la stessa istanza di objB o se entrambi sono Null; in caso contrario, false.

Esempio

Nell'esempio seguente viene ReferenceEquals utilizzato per determinare se due oggetti sono la stessa istanza.

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
'

Commenti

A differenza del metodo e dell'operatore Equals di uguaglianza, il ReferenceEquals metodo non può essere sottoposto a override. Per questo motivo, se si desidera testare due riferimenti a oggetti per verificarne l'uguaglianza e non si è certi dell'implementazione del Equals metodo , è possibile chiamare il ReferenceEquals metodo .

Tuttavia, il valore restituito del ReferenceEquals metodo potrebbe sembrare anomalo in questi due scenari:

  • Quando si confrontano i tipi di valore. Se objA e objB sono tipi valore, vengono boxed prima che vengano passati al ReferenceEquals metodo . Ciò significa che se entrambi objA e objB rappresentano la stessa istanza di un tipo valore, il ReferenceEquals metodo restituisce falsecomunque , come illustrato nell'esempio seguente.

    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
    

    Per informazioni sui tipi valore boxing, vedere Boxing e Unboxing.

  • Quando si confrontano stringhe. Se objA e objB sono stringhe, il ReferenceEquals metodo restituisce true se la stringa viene internata. Non esegue un test per verificare l'uguaglianza dei valori. Nell'esempio s1 seguente e s2 sono uguali perché sono due istanze di una singola stringa internata. Tuttavia, s3 e s4 non sono uguali, perché anche se hanno valori stringa identici, tale stringa non viene internata.

    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
    

    Per altre informazioni sull'interno delle stringhe, vedere String.IsInterned.

Si applica a

Vedi anche