Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Diving into Equals Method vs. == operator (I)

0.00/5 (No votes)
18 Nov 2016 2  
Equals method vs. == operator

Some days ago, I was debugging the code of one of my company's applications trying to solve a bug. Eventually, I found the origin of the mistake and I thought that it would be interesting to write a little post about it. It is an issue that generates a lot of confusion, so I will try to make clear the differences between "Equals" method and "==" operator when dealing with reference types.

Without diving a lot into the bug, there was an error in code when comparing equality for two different objects (type “Object”!), in particular stored values in different versions of a DataColumn (with DateTime type in my case) in the same DataRow. An equality "==" operator was being used to make the comparison between them and as a consequence, the result was always "false" despite the stored value in both versions of the row was the same.

The incorrect code was similar to:

C#
bool isEqual=(ds.Tables[0].Rows[0]["ColName"]==
ds.Tables[0].Rows[0]["ColName",DataViewRowState.OriginalRows])

For reference types where "==" has not been overloaded, it compares whether two references refer to the same object. In the code above, the references are different in spite of containing the same value. So, the variable isEqual is always false.

To solve the problem, there are several options, for instance:

  1. Use "Equals" method instead of "==" operator.
    C#
    bool isEqual=(ds.Tables[0].Rows[0]["ColName"].Equals
    (ds.Tables[0].Rows[0]["ColName",DataViewRowState.OriginalRows)

    Equals method compares underlying stored values, not references.

  2. Cast the objects to the expected stored type (DateTime in my case) and then make the comparison using "==" operator.
    C#
    bool isEqual=((DateTime)ds.Tables[0].Rows[0]["ColName"]==
    ((DateTime)ds.Tables[0].Rows[0]["ColName",DataViewRowState.OriginalRows)

    DateTime type overloads "==" operator and then it compares values, not references.

All in all, I prefer the first one because a rule of thumb for me is using Equals method for almost all references types when I want to test equality rather than reference identity. There are some exceptions, for instance when I work with String objects I like using "==" operator for better readability. The comparison keeps being correct owing to the overload of the operator in the String type. It is important to know that in this case, both sides of the operator must be expressions of type String in order to get the comparison to work properly.

In part II of this post, I will set out a new detailed code example to clarify and make this issue more comprehensive.

You can get more information here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here