Object.Equals Method (Object, Object)
Determines whether the specified object instances are considered equal.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- objA
- Type: System.Object
The first object to compare.
- objB
- Type: System.Object
The second object to compare.
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 */
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.
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.]