Object.ReferenceEquals(Object, Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
确定指定的 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
要比较的第二个对象。
返回
如果 objA
是与 objB
相同的实例,或如果两者均为 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 返回值似乎异常:
比较值类型时。 如果
objA
并且objB
是值类型,则会在将它们传递到 ReferenceEquals 方法之前进行装箱。 这意味着,如果同时objA``objB
表示值类型的同一实例,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
有关装箱值类型的信息,请参阅 装箱和取消装箱。
比较字符串时。 如果
objA
字符串和objB
字符串为字符串,该方法 ReferenceEquals 将true
返回字符串是实习生的。 它不对值相等性执行测试。 在下面的示例中,s1
并且s2
相等,因为它们是单个实习生字符串的两个实例。 但是,s3
并且s4
不相等,因为它们虽然具有相同的字符串值,但该字符串不是实习生。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。