1 out of 5 rated this helpful - Rate this topic

Object.Equals Method (Object, Object)

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 the objects are considered equal; 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 MyClass {
   public static void Main() {
   string s1 = "Tom";
   string s2 = "Carol";
   Console.WriteLine("Object.Equals(\"{0}\", \"{1}\") => {2}", 
      s1, s2, Object.Equals(s1, s2));

   s1 = "Tom";
   s2 = "Tom";
   Console.WriteLine("Object.Equals(\"{0}\", \"{1}\") => {2}", 
      s1, s2, Object.Equals(s1, s2));

   s1 = null;
   s2 = "Tom";
   Console.WriteLine("Object.Equals(null, \"{1}\") => {2}",
       s1, s2, Object.Equals(s1, s2));

   s1 = "Carol";
   s2 = null;
   Console.WriteLine("Object.Equals(\"{0}\", null) => {2}", 
       s1, s2, Object.Equals(s1, s2));

   s1 = null;
   s2 = null;
   Console.WriteLine("Object.Equals(null, null) => {2}", 
       s1, s2, Object.Equals(s1, s2));
   }
}


/*

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

*/


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Behavior of Object.Equals
The "Remarks" are a bit misleading: Object.Equals(Object, Object) is a static method, and, thus, it cannot be "overridden by a derived type".

What Object.Equals(objA, objB) actually does is to (a) check for reference equality and, if reference equality does not hold, (b) call objA.Equals(objB). So, in fact, the result of Object.Equals(objA, objB) depends on the concrete implementation of (the non-static, virtual) Object.Equals(Object) in objA's type. [And that's actually the information I'd like to see here in the documentation.]