Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Object.ReferenceEquals Method

Determines whether the specified Object instances are the same instance.

[Visual Basic]
Public Shared Function ReferenceEquals( _
   ByVal objA As Object, _
   ByVal objB As Object _
) As Boolean
[C#]
public static bool ReferenceEquals(
 object objA,
 object objB
);
[C++]
public: static bool ReferenceEquals(
 Object* objA,
 Object* objB
);
[JScript]
public static function ReferenceEquals(
   objA : Object,
 objB : Object
) : Boolean;

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

[C#, C++, JScript] 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 <mscorlib.dll>
using namespace System;

int main() {
   Object* o = 0;
   Object* p = 0;
   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

*/

[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

*/

[Visual Basic] No example is available for Visual Basic. To view a C#, C++, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Object Class | Object Members | System Namespace | Equals

Show:
© 2015 Microsoft