Object.ReferenceEquals(Object, Object) Object.ReferenceEquals(Object, Object) Object.ReferenceEquals(Object, Object) Object.ReferenceEquals(Object, Object) Method

定义

确定指定的 Object 实例是否是相同的实例。Determines whether the specified Object instances are the same instance.

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

参数

objA
Object Object Object Object

要比较的第一个对象。The first object to compare.

objB
Object Object Object Object

要比较的第二个对象。The second object to compare.

返回

如果 objA 是与 objB 相同的实例,或两者均为 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.

True
True
False

*/
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

*/
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
'

注解

与方法和相等运算符不同ReferenceEquals ,方法不能被重写。 EqualsUnlike 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 ReferenceEqualsobjB是值类型,则在将其传递给方法之前将它们装箱。If 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.

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

    有关装箱值类型的信息,请参阅装箱和取消装箱For information on boxing value types, see Boxing and Unboxing.

  • 比较字符串时。When comparing strings. 如果objA ReferenceEquals trueobjB是字符串,则在暂存字符串时,方法将返回。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 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
    
    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请参阅。For more information about string interning, see String.IsInterned.

适用于

另请参阅