Export (0) Print
Expand All

Object.ReferenceEquals Method

Determines whether the specified Object instances are the same instance.

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

public static bool ReferenceEquals (
	Object objA,
	Object objB
)
public static boolean ReferenceEquals (
	Object objA, 
	Object objB
)
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.

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

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

*/

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

*/

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

*/

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft