Click here to Skip to main content
15,888,351 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
18 Nov 2016CPOL2 min read 13.5K   3   4
"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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Spain Spain
Telecommunication Engineer by University of Zaragoza, Spain.

Passionate Software Architect, MCPD, MCAD, MCP. Over 10 years on the way building software.

Mountain Lover, Popular Runner, 3km 8'46, 10km 31'29, Half Marathon 1h08'50, Marathon 2h27'02. I wish I could be able to improve, but It's difficult by now

Comments and Discussions

 
QuestionDateTime and reference types... Pin
Paulo Zemek18-Nov-16 8:42
mvaPaulo Zemek18-Nov-16 8:42 
AnswerRe: DateTime and reference types... Pin
jamuro7718-Nov-16 9:49
jamuro7718-Nov-16 9:49 
BugString should not compared with == Pin
N.Mayer18-Nov-16 5:35
N.Mayer18-Nov-16 5:35 
GeneralRe: String should not compared with == Pin
jamuro7718-Nov-16 6:20
jamuro7718-Nov-16 6:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.