Object.ReferenceEquals(Object, Object) Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Określa, czy określone Object wystąpienia są tym samym wystąpieniem.
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.
- objB
- Object
Drugi obiekt do porównania.
Zwraca
true jeśli objA jest to to samo wystąpienie co objB lub jeśli oba są null; w przeciwnym razie false.
Przykłady
W poniższym przykładzie użyto ReferenceEquals funkcji do określenia, czy dwa obiekty są tym samym wystąpieniem.
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
'
Uwagi
Equals W przeciwieństwie do metody i operatora ReferenceEquals równości nie można zastąpić metody. W związku z tym, jeśli chcesz przetestować dwa odwołania do obiektów pod kątem równości i nie masz pewności co do implementacji Equals metody, możesz wywołać metodę ReferenceEquals .
Jednak zwracana wartość ReferenceEquals metody może wydawać się nietypowa w tych dwóch scenariuszach:
Podczas porównywania typów wartości. Jeśli
objAiobjBsą typami wartości, są one pola przed przekazaniem ReferenceEquals ich do metody. Oznacza to, że jeśli obaobjBobjAi reprezentują to samo wystąpienie typu wartości, ReferenceEquals metoda jednak zwracafalsemetodę , jak pokazano w poniższym przykładzie.int int1 = 3; Console.WriteLine(Object.ReferenceEquals(int1, int1)); Console.WriteLine(int1.GetType().IsValueType); // The example displays the following output: // False // Truelet int1 = 3 printfn $"{Object.ReferenceEquals(int1, int1)}" printfn $"{int1.GetType().IsValueType}" // The example displays the following output: // False // TruePublic 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 ' TrueAby uzyskać informacje na temat typów wartości boksu, zobacz Boxing and Unboxing (Boxing and Unboxing).
Podczas porównywania ciągów. Jeśli
objAciągi iobjBsą ciągami, metoda zwracatruewartość , ReferenceEquals jeśli ciąg jest internowany. Nie wykonuje testu równości wartości. W poniższym przykładzie sąs2równe,s1ponieważ są to dwa wystąpienia pojedynczego internowanego ciągu. Jednak is4nie są równe,s3ponieważ chociaż mają identyczne wartości ciągów, ten ciąg nie jest internowany.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: Noopen 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: NoModule 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: NoAby uzyskać więcej informacji na temat interningu ciągów, zobacz String.IsInterned.