Click here to Skip to main content
12,630,420 members (29,586 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

68.7K views
12 bookmarked
Posted

What is the Difference between “==” and .Equals()?

, 26 Apr 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
In this article, we will discuss what is the difference between "==" vs ".Equals in C#".

When we create any object, there are two parts to the object, one is the content and the other is reference to that content.

So for example, if you create an object as shown in the below code:

  1. “.NET interview questions” is the content.
  2. o” is the reference to that content.
object o = ".NET Interview questions"; 

==” compares if the object references are the same while “.Equals()” compares if the contents are the same.

So if you run the below code, both “==” and “.Equals()” returns true because content as well as references are the same.

 

object o = ".NET Interview questions";
object o1 = o;
Console.WriteLine(o == o1);
Console.WriteLine(o.Equals(o1));
Console.ReadLine();

True

True

Now consider the below code where we have the same content but they point towards different instances. So if you run the below code, both “==” will return false and “.Equals()” will return true.

object o = ".NET Interview questions";
object o1 = new string(".NET Interview questions".ToCharArray());
Console.WriteLine(o == o1);
Console.WriteLine(o.Equals(o1));
Console.ReadLine();

False

True

When you are using string data type, it always does content comparison. In other words, you either use “.Equals()” or “==” it always does content comparison.

You can also watch the following video of the above explanation at C# interview questions and answers: Difference between "==" and ".Equals()"?

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

Comments and Discussions

 
QuestionNice Article Pin
poonamkalra0918-Feb-16 23:02
memberpoonamkalra0918-Feb-16 23:02 
GeneralMessage Removed Pin
Sopan Maiti2-Jul-15 21:04
memberSopan Maiti2-Jul-15 21:04 
QuestionDiffcult to understand Pin
madan singh bora26-Jun-15 18:52
membermadan singh bora26-Jun-15 18:52 
QuestionAlways == does not do the content comparison for string Pin
gourav.agrawal28-Jan-15 2:45
membergourav.agrawal28-Jan-15 2:45 
GeneralClear! Pin
nicolas mertens20-Jun-14 2:35
membernicolas mertens20-Jun-14 2:35 
QuestionGood Explanation Pin
Member 1028643326-Feb-14 3:35
memberMember 1028643326-Feb-14 3:35 
Blush | :O Keep it up
GeneralMy vote of 5 Pin
M Rayhan3-Jan-14 18:51
memberM Rayhan3-Jan-14 18:51 
GeneralMy vote of 3 Pin
John Brett28-Apr-13 23:19
memberJohn Brett28-Apr-13 23:19 
GeneralRe: My vote of 3 Pin
Shivprasad koirala2-May-13 20:52
mvpShivprasad koirala2-May-13 20:52 
Generalsmth wrong Pin
mythteria26-Apr-13 22:32
membermythteria26-Apr-13 22:32 
GeneralRe: smth wrong Pin
Shivprasad koirala26-Apr-13 22:54
mvpShivprasad koirala26-Apr-13 22:54 
GeneralRe: smth wrong Pin
mythteria29-May-13 3:01
membermythteria29-May-13 3:01 
GeneralRe: smth wrong Pin
Member 104448254-Dec-13 13:12
memberMember 104448254-Dec-13 13:12 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161205.3 | Last Updated 27 Apr 2013
Article Copyright 2013 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid