Object.ReferenceEquals(Object, Object) Metoda
Definicja
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
Parametry
- objA
- Object
Pierwszy obiekt do porównania.The first object to compare.
- objB
- Object
Drugi obiekt do porównania.The second object to compare.
Zwraca
true
Jeśli objA
jest to to samo wystąpienie co objB
lub jeśli oba mają wartość null; w przeciwnym razie false
.true
if objA
is the same instance as objB
or if both are null; otherwise, false
.
Przykłady
Poniższy przykład używa ReferenceEquals do określenia, czy dwa obiekty są tego samego wystąpienia.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
*/
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
'
Uwagi
W przeciwieństwie do Equals metody i operatora równości ReferenceEquals nie można zastąpić metody.Unlike the Equals method and the equality operator, the ReferenceEquals method cannot be overridden. W związku z tym, jeśli chcesz przetestować dwa odwołania do obiektów pod kątem równości i nie wiesz, jak wdrażać Equals
metodę, możesz wywołać ReferenceEquals metodę.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.
Jednak wartość zwracana ReferenceEquals metody może wydawać się nieanomalia w tych dwóch scenariuszach:However, the return value of the ReferenceEquals method may appear to be anomalous in these two scenarios:
Podczas porównywania typów wartości.When comparing value types. Jeśli
objA
iobjB
są typami wartości, są one opakowane przed przekazaniem ich do ReferenceEquals metody.IfobjA
andobjB
are value types, they are boxed before they are passed to the ReferenceEquals method. Oznacza to, że jeśli obaobjA
iobjB
reprezentują to samo wystąpienie typu wartości, ReferenceEquals Metoda Jednakże zwracafalse
, jak pokazano w poniższym przykładzie.This means that if bothobjA
andobjB
represent the same instance of a value type, the ReferenceEquals method nevertheless returnsfalse
, as the following example shows.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
Aby uzyskać informacje o typach wartości opakowania, zobacz opakowanie i rozpakowywanie.For information on boxing value types, see Boxing and Unboxing.
Podczas porównywania ciągów.When comparing strings. Jeśli
objA
iobjB
są ciągami, ReferenceEquals Metoda zwraca,true
Jeśli ciąg jest InterNIC.IfobjA
andobjB
are strings, the ReferenceEquals method returnstrue
if the string is interned. Nie wykonuje testu pod kątem równości wartości.It does not perform a test for value equality. W poniższym przykładzies1
is2
są równe, ponieważ są dwa wystąpienia jednego ciągu z stażystami.In the following example,s1
ands2
are equal because they are two instances of a single interned string. Jednaks3
is4
nie są równe, ponieważ chociaż mają identyczne wartości ciągu, ten ciąg nie jest InterNIC.However,s3
ands4
are not equal, because although they have identical string values, that string is not interned.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
Aby uzyskać więcej informacji na temat informowania o ciągach, zobacz String.IsInterned .For more information about string interning, see String.IsInterned.