Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
Object Class
Object Methods
Equals Method
Members FilterMembers Filter
Frameworks FilterFrameworks Filter
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Object..::.Equals Method

Updated: November 2007

Determines whether two Object instances are equal.

  NameDescription
Equals(Object) Determines whether the specified Object is equal to the current Object.
Equals(Object, Object) Determines whether the specified Object instances are considered equal.
Top
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
This tests instance equality      Will Sullivan ... lambertwm   |   Edit   |  
-- IMHO this is incorrect, lambertwm --

if

object foo = new object();
object bar = foo;

then

object.Equals(foo, bar)

evaluates to true. Variables foo and bar both point to the same object on the managed heap. If we were to say

StringBuilder foo = new StringBuilder();
foo.Append("lol");
StringBuilder bar= new StringBuilder();
bar.Append("lol");

and then examine the memory locations for foo and bar, they would be indistinguishable. The only difference between the two is that they are seperate objects on the managed heap. In this case, even though we would consider them to be the same, as they have the exact same content,

object.Equals(foo, bar)

would evaluate to false because they are two different objects.

It is the responsibility of coders to override the operators == and != and to provide overrides for both Equals methods if you want to change this behavior so that you compare the states of two objects rather than the instances of the objects when testing for equality.
The content above ("This tests instance equality") is incorrect      lambertwm   |   Edit   |  
It confuses Object.Equals( ) with Object.ReferenceEquals( )
Tags What's this?: Add a tag
Flag as ContentBug
Correct Explanation      prahaladd   |   Edit   |  
The Object.Equals method tests whether the CONTENT of two objects are the same irrespective of whether the two objects are referring to the same memory location on the heap or not

Hence the below code snippet:

StringBuilder foo = new StringBuilder();
foo.Append("lol");
StringBuilder bar= new StringBuilder();
bar.Append("lol");

foo.Equals(bar)


will always return true since both foo and bar have the CONTENT as "lol"


However the following will return false:

Object.ReferenceEquals(foo,bar)

since foo and bar are two SEPARATE objects on the heap and hence their memory references are not the same.

The exception to the above is in case of strings.

Consider the following code snippet:

string

s1 = "Hello";

string s2 = "Hello";

Console.WriteLine(s1.Equals(s2)); // this returns true since both s1 and s2 contain the string 'Hello'

Console.WriteLine(Object.ReferenceEquals(s1, s2)); // this also returns true!!


Here the Object.ReferenceEquals returns true since although s1 and s2 are two different string objects; .NET CLR handles strings in a special way and according to my understanding uses string pooling wherein two strings that have the same content will point to the same memory location.


Let me know if there is any misunderstanding on my side with respect to the above statement

I'm Confused here. The MS Documentation states differently      Equals   |   Edit   |  
MS doc says Equals tests reference equality is this a misprint?
It's obvious that Equals test DOES test content.

"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."
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker