다음을 통해 공유


Object.ReferenceEquals Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: October 2010

Determines whether the specified object instances are the same instance.

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

Syntax

'Declaration
Public Shared Function ReferenceEquals ( _
    objA As Object, _
    objB As Object _
) As Boolean
public static bool ReferenceEquals(
    Object objA,
    Object objB
)

Parameters

Return Value

Type: System.Boolean
true if objA is the same instance as objB or if both are nulla null reference (Nothing in Visual Basic); otherwise, false.

Remarks

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.

Examples

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


Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
    Dim o As Object = Nothing
    Dim p As Object = Nothing
    Dim q As New Object
    outputBlock.Text += Object.ReferenceEquals(o, p) & vbCrLf
    p = q
    outputBlock.Text += Object.ReferenceEquals(p, q) & vbCrLf
    outputBlock.Text += Object.ReferenceEquals(o, p) & vbCrLf
   End Sub
End Class
' This code produces the following output:
'
' True
' True
' False
'
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.

True
True
False

*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Change History

Date

History

Reason

October 2010

Added the Remarks section.

Customer feedback.