Freigeben über


Object.Equals Method (Object, Object)

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

Updated: December 2010

Determines whether the specified Object instances are considered equal.

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

Syntax

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

Parameters

Return Value

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

Remarks

The default implementation of Equals supports reference equality for reference types, and bitwise equality for value types. Reference equality means the object references that are compared refer to the same object. Bitwise equality means the objects that are compared have the same binary representation.

Note that a derived type might override the Equals method to implement value equality. Value equality means the compared objects have the same value but different binary representations.

For more information, see the Object.Equals(Object) topic.

Examples

The following code example compares different objects.


Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim s1 As String = "Tom"
      Dim s2 As String = "Carol"
      outputBlock.Text += String.Format("Object.Equals(""{0}"", ""{1}"") => {2}", s1, s2, [Object].Equals(s1, s2)) & vbCrLf

      s1 = "Tom"
      s2 = "Tom"
      outputBlock.Text += String.Format("Object.Equals(""{0}"", ""{1}"") => {2}", s1, s2, [Object].Equals(s1, s2)) & vbCrLf

      s1 = Nothing
      s2 = "Tom"
      outputBlock.Text += String.Format("Object.Equals(null, ""{1}"") => {2}", s1, s2, [Object].Equals(s1, s2)) & vbCrLf

      s1 = "Carol"
      s2 = Nothing
      outputBlock.Text += String.Format("Object.Equals(""{0}"", null) => {2}", s1, s2, [Object].Equals(s1, s2)) & vbCrLf

      s1 = Nothing
      s2 = Nothing
      outputBlock.Text += String.Format("Object.Equals(null, null) => {2}", s1, s2, [Object].Equals(s1, s2)) & vbCrLf

   End Sub 'Main
End Class '[MyClass]
'
'This code produces the following output.
'
'Object.Equals("Tom", "Carol") => False
'Object.Equals("Tom", "Tom") => True
'Object.Equals(null, "Tom") => False
'Object.Equals("Carol", null) => False
'Object.Equals(null, null) => True
'
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string s1 = "Tom";
      string s2 = "Carol";
      outputBlock.Text += String.Format("Object.Equals(\"{0}\", \"{1}\") => {2}",
         s1, s2, Object.Equals(s1, s2)) + "\n";

      s1 = "Tom";
      s2 = "Tom";
      outputBlock.Text += String.Format("Object.Equals(\"{0}\", \"{1}\") => {2}",
         s1, s2, Object.Equals(s1, s2)) + "\n";

      s1 = null;
      s2 = "Tom";
      outputBlock.Text += String.Format("Object.Equals(null, \"{1}\") => {2}",
          s1, s2, Object.Equals(s1, s2)) + "\n";

      s1 = "Carol";
      s2 = null;
      outputBlock.Text += String.Format("Object.Equals(\"{0}\", null) => {2}",
          s1, s2, Object.Equals(s1, s2)) + "\n";

      s1 = null;
      s2 = null;
      outputBlock.Text += String.Format("Object.Equals(null, null) => {2}",
          s1, s2, Object.Equals(s1, s2)) + "\n";
   }
}


/*

This code produces the following output.

Object.Equals("Tom", "Carol") => False
Object.Equals("Tom", "Tom") => True
Object.Equals(null, "Tom") => False
Object.Equals("Carol", null) => False
Object.Equals(null, null) => True

*/

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.

Change History

Date

History

Reason

December 2010

Removed exception information.

Content bug fix.