Click here to Skip to main content
Click here to Skip to main content

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

By , 26 Apr 2013
 
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 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 same while “.Equals()” compares if the contents are same.
So if you run the below code both “==” and “.Equals()” returns true because content as well as references are 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 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 do 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)

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India
Member

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 3memberJohn Brett28 Apr '13 - 22:19 
GeneralRe: My vote of 3mvpShivprasad koirala2 May '13 - 19:52 
Generalsmth wrongmembermythteria26 Apr '13 - 21:32 
GeneralRe: smth wrongmvpShivprasad koirala26 Apr '13 - 21:54 

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

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