Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.
Traduzione
Inglese

Metodo Object.ReferenceEquals (Object, Object)

 

Determina se le istanze di Object specificate rappresentano la stessa istanza.

Spazio dei nomi:   System
Assembly:  mscorlib (in mscorlib.dll)

public static bool ReferenceEquals(
	object objA,
	object objB
)

Parametri

objA
Type: System.Object

Il primo oggetto da confrontare.

objB
Type: System.Object

Secondo oggetto da confrontare.

Valore restituito

Type: System.Boolean

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

Unlike the M:System.Object.Equals(System.Object) method and the equality operator, the M:System.Object.ReferenceEquals(System.Object,System.Object) 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 M:System.Object.ReferenceEquals(System.Object,System.Object) method.

However, the return value of the M:System.Object.ReferenceEquals(System.Object,System.Object) 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 M:System.Object.ReferenceEquals(System.Object,System.Object) method. This means that if both objA and objB represent the same instance of a value type, the M:System.Object.ReferenceEquals(System.Object,System.Object) 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
    

    For information on boxing value types, see Boxing and Unboxing (C# Programming Guide).

  • When comparing strings. If objA and objB are strings, the M:System.Object.ReferenceEquals(System.Object,System.Object) 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("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 M:System.String.IsInterned(System.String).

The following example uses M:System.Object.ReferenceEquals(System.Object,System.Object) 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

*/

Universal Windows Platform
Disponibile da 8
.NET Framework
Disponibile da 1.1
Libreria di classi portabile
Supportato in: piattaforme .NET portabili
Silverlight
Disponibile da 2.0
Windows Phone Silverlight
Disponibile da 7.0
Windows Phone
Disponibile da 8.1
Torna all'inizio
Mostra: