Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Object.Equals Method (Object, Object)

Updated: December 2010

Determines whether the specified Object instances are considered equal.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static bool Equals(
	Object objA,
	Object objB
)

Parameters

objA
Type: System.Object
The first Object to compare.
objB
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 references or if objA.Equals(objB) returns true; otherwise, false.

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.

The following code example compares different objects.


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

*/


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

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

Date

History

Reason

December 2010

Removed exception information.

Content bug fix.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.