Object.ReferenceEquals(Object, 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
비교할 첫 번째 개체입니다.The first object to compare.
- objB
- Object
비교할 두 번째 개체입니다.The second object to compare.
반환
objA
와 objB
의 인스턴스가 같거나 둘 다 null 인 경우 true
이고 그렇지 않으면 false
입니다.true
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
*/
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
'
설명
Equals메서드와 같음 연산자와 달리 ReferenceEquals 메서드는 재정의할 수 없습니다.Unlike 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
및objB
가 값 형식인 경우 메서드로 전달 되기 전에 boxing 됩니다 ReferenceEquals .IfobjA
andobjB
are value types, they are boxed before they are passed to the ReferenceEquals method. 즉,와가 모두objA
objB
값 형식의 동일한 인스턴스를 나타내는 경우 ReferenceEqualsfalse
다음 예제와 같이 메서드는를 반환 합니다.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
Boxing 값 형식에 대 한 자세한 내용은 boxing 및 Unboxing을 참조 하세요.For information on boxing value types, see Boxing and Unboxing.
문자열을 비교할 때.When comparing strings.
objA
및objB
가 문자열인 경우이 ReferenceEquals 메서드는true
문자열이 풀에 있는 경우를 반환 합니다.IfobjA
andobjB
are strings, the ReferenceEquals method returnstrue
if the string is interned. 값 일치에 대 한 테스트를 수행 하지 않습니다.It does not perform a test for value equality. 다음 예제에서s1
및s2
는 단일 인턴 문자열의 두 인스턴스인 경우와 동일 합니다.In the following example,s1
ands2
are equal because they are two instances of a single interned string. 그러나s3
와는s4
동일한 문자열 값을 가지 지만 해당 문자열은 인턴이 아닙니다.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
문자열 인턴에 대 한 자세한 내용은을 참조 하십시오 String.IsInterned .For more information about string interning, see String.IsInterned.