ReferenceEquals Method
Collapse the table of content
Expand the table of content

Object.ReferenceEquals Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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


Type: System.Object
The first object to compare.
Type: System.Object
The second object to compare.

Return Value

Type: System.Boolean
true if objA is the same instance as objB or if both are null; otherwise, false.

Unlike the Equals method and the equality operator, the ReferenceEquals method cannot be overridden. Because of this, if you want to test two object references for equality and are unsure about the implementation of the Equals method, you can call the ReferenceEquals method. However, note that if objA and objB are value types, they are boxed before they are passed to the ReferenceEquals method.

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

using System;

class Example

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
        object o = null;
        object p = null;
        object q = new Object();

        outputBlock.Text += Object.ReferenceEquals(o, p) + "\n";
        p = q;
        outputBlock.Text += Object.ReferenceEquals(p, q) + "\n";
        outputBlock.Text += Object.ReferenceEquals(o, p) + "\n";


This code produces the following output.



Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2017 Microsoft