# Object.ReferenceEquals(Object, Object)Object.ReferenceEquals(Object, Object)Object.ReferenceEquals(Object, Object)Object.ReferenceEquals(Object, Object) Method

## Definição

Determina se as instâncias de Object especificadas são a mesma instância.Determines whether the specified Object instances are the same instance.

public:
static bool ReferenceEquals(System::Object ^ objA, System::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

#### Parâmetros

objA
Object Object Object Object

O primeiro objeto a ser comparado.The first object to compare.

objB
Object Object Object Object

O segundo objeto a ser comparado.The second object to compare.

#### Retornos

true se objA for a mesma instância que objB ou se ambos forem null; caso contrário, false.true if objA is the same instance as objB or if both are null; otherwise, false.

## Exemplos

O exemplo a seguir usa ReferenceEquals para determinar se dois objetos são a mesma instância.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

*/
using System;

class MyClass {

static void Main() {
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

*/
Imports System

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
'

## Comentários

Ao contrário do Equals método e o operador de igualdade, o ReferenceEquals método não pode ser substituído.Unlike the Equals method and the equality operator, the ReferenceEquals method cannot be overridden. Por isso, se você quiser testar duas referências de igualdade do objeto e você não tiver certeza sobre a implementação do Equals método, você pode chamar o ReferenceEquals método.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.

No entanto, o valor de retorno de ReferenceEquals método pode aparecer como anormais nesses dois cenários:However, the return value of the ReferenceEquals method may appear to be anomalous in these two scenarios:

• Ao comparar tipos de valor.When comparing value types. Se objA e objB são tipos de valor, eles são boxed antes de serem passados para o ReferenceEquals método.If objA and objB are value types, they are boxed before they are passed to the ReferenceEquals method. Isso significa que, se os dois objA e objB representam a mesma instância de um tipo de valor, o ReferenceEquals mesmo assim, o método retorna false, como mostra o exemplo a seguir.This means that if both objA and objB represent the same instance of a value type, the ReferenceEquals method nevertheless returns false, as the following example shows.

using System;

public class Example
{
public static void Main()
{
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

Para obter informações sobre tipos de valor de conversão boxing, consulte conversão Boxing e Unboxing.For information on boxing value types, see Boxing and Unboxing.

• Ao comparar cadeias de caracteres.When comparing strings. Se objA e objB são cadeias de caracteres, o ReferenceEquals retorno do método true se a cadeia de caracteres é internalizada.If objA and objB are strings, the ReferenceEquals method returns true if the string is interned. Ele não executa um teste de igualdade de valor.It does not perform a test for value equality. No exemplo a seguir s1 e s2 são iguais, porque eles são duas instâncias de uma única cadeia de caracteres interno.In the following example, s1 and s2 are equal because they are two instances of a single interned string. No entanto, s3 e s4 não forem iguais, porque embora são tiverem valores de cadeia de caracteres idêntica, essa cadeia de caracteres não é interno.However, s3 and s4 are not equal, because although they are have identical string values, that string is not interned.

using System;

public class Example
{
public static void Main()
{
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