Object.ReferenceEquals Method (System)

Switch View :
ScriptFree
Object.ReferenceEquals Method
Determines whether the specified Object instances are the same instance.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax

Visual Basic (Declaration)
Public Shared Function ReferenceEquals ( _
	objA As Object, _
	objB As Object _
) As Boolean
Visual Basic (Usage)
Dim objA As Object
Dim objB As Object
Dim returnValue As Boolean

returnValue = Object.ReferenceEquals(objA, objB)
C#
public static bool ReferenceEquals (
	Object objA,
	Object objB
)
C++
public:
static bool ReferenceEquals (
	Object^ objA, 
	Object^ objB
)
J#
public static boolean ReferenceEquals (
	Object objA, 
	Object objB
)
JScript
public static function ReferenceEquals (
	objA : Object, 
	objB : Object
) : boolean
XAML
Not applicable.

Parameters

objA

The first Object to compare.

objB

The second Object to compare.

Return Value

true if objA is the same instance as objB or if both are null references; otherwise, false.
Example

The following code example uses ReferenceEquals to determine if two objects are the same instance.

C#
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

*/

C++
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

*/

J#
import System.*;

class MyClass
{
    public static void main(String[] args)
    {
        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));
    } //main
} //MyClass

/*

This code produces the following output.

True
True
False

*/

JScript
import System

class MyClass {

   static function Main() {
      var o = null;
      var p = null;
      var q = new Object();

      Console.WriteLine(Object.ReferenceEquals(o, p));
      p = q;
      Console.WriteLine(Object.ReferenceEquals(p, q));
      Console.WriteLine(Object.ReferenceEquals(o, p));
   }
}

MyClass.Main();

/*

This code produces the following output.

True
True
False

*/

Platforms

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

Version Information

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0
See Also

Community Content

Thomas Lee
ReferenceEquals Method Using PowerShell
# refeq-obj.ps1
# Sample showing referencequals static method on object class
# Thomas Lee - tfl@psp.co.uk

# Create three objects
$i = new-object system.object
$j = new-object system.object
$k = $i

# now use referenceequals and print results

"`$i=`$j: {0}" -f [system.int16]::referenceequals($i, $j)
"`$j=`$k: {0}" -f [system.int16]::referenceequals($j, $k)
"`$k=`$i: {0}" -f [system.int16]::referenceequals($i, $k)

This script produces the following output

PSH [D:\foo]: .\refeq-int16.ps1
$i=$j: False
$j=$k: False
$k=$i: True