Object.ReferenceEquals(Object, 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

비교할 첫 번째 개체입니다.

objB
Object

비교할 두 번째 개체입니다.

반환

objAobjB의 인스턴스가 같거나 둘 다 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 다음 두 가지 시나리오에서 비정상적인 것처럼 보일 수 있습니다.

  • 값 형식을 비교할 때 및 objB 가 값 형식인 경우 objA 메서드에 전달 ReferenceEquals 되기 전에 상자에 포함됩니다. 즉, 및 objB 가 모두 objA 값 형식 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
    

    boxing 값 형식에 대한 자세한 내용은 Boxing 및 Unboxing을 참조하세요.

  • 문자열을 비교할 때 및 objB 가 문자열인 경우 objA 메서드는 ReferenceEquals 문자열이 인턴되는 경우 를 반환합니다true. 값 같음 테스트를 수행하지 않습니다. 다음 예제 s1 에서 및 s2 는 인턴된 단일 문자열의 두 인스턴스이므로 동일합니다. 그러나 s3s4 는 같지 않습니다. 문자열 값이 동일하지만 해당 문자열은 인턴되지 않기 때문입니다.

    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.

적용 대상

추가 정보