What is the difference between a.Equals(b) and a == b?
For value types: “==” and Equals() works same way : Compare two objects by VALUE
Example:
int i = 5; int k= 5; Here “==” and Equals() is as:
i == k >> True
i.Equals(k) >> True
For reference types : both works differently :
“==” compares reference – returns true if and only if both references point to the SAME object while
"Equals" method compares object by VALUE and it will return true if the references refers object which are equivalent
Example:
StringBuilder objSb1 = new
StringBuilder("Elias"); StringBuilder objSb2 = new
StringBuilder("Elias");
Here “==” and Equals() is as: objSb1 == objSb2 >> False
objSb1.Equals(objSb2) >> True
But now look at following issue:
string s1 = "Elias"; string s2 =
"Elias";
Here “==” and Equals() is as: In above case the results will be,
s1 == s2 >> True
s1.Equals(s2) >> True
String is actually reference type: its a sequence of “Char” and its also immutable but as you saw above, it will behave like Value Types in this case.
Exceptions are always there ;)
Now another interesting case: int i = 0; byte b = 0;
Here “==” and Equals() is as: i == b >> True
i.Equals(b) >> False
So, it means Equals method compare not only value but it compares TYPE also in Value Types.
Recommendation: For value types: use “==”
For reference types: use Equals method.
Md. Elias Hossain
MCP, MCTS(WEB)
Software Engineer
// now the output is - true true
//Now make some changes here object obj1
= sStr1;
object obj2 = sStr2;
Response.Write(obj1 == obj2);
Response.Write(obj1.Equals(obj2));
//here the output is true true false true
}
Note - > obj1==obj2 is false because compiler doesnot know the contents of obj1 & obj2.So they are references to different - 2 strings, so the operators returns false.
and http://msdn.microsoft.com/en-us/library/system.object.gethashcode%28VS.71%29.aspx
Well, when you do obj1.equal(obj2) you are using it for value comparision and not for
reference identity.
But now look at following issue:
string s1 = "Elias";
string s2 = "Elias";
Here “==” and Equals() is as:
In above case the results will be,
s1 == s2 >> True
s1.Equals(s2) >> True
But here, A String object is
called immutable (read-only) because its value cannot be modified once
it has been created. Methods that appear to modify a String object actually return a new String object that contains the modification.
There is something called "String literal pool".
When you make a string object say Str1 = "123"
it goes in this pool.
Now you prepare second object as Str2 = "123"
new object might not get prepared as it already exist in pool
Instead it points to same reference.
So,
str1.Equals(str2) >> True
Another thing StringBuilder is Mutable object i.e
if you do
StringBuilder StBlObj = new StringBuilder();
StBlObj = "123";
StBlObj = "123245";
Same object gets updated , no new object is created.
Please refer to this link : http://en.csharp-online.net/CSharp_String_Theory%E2%80%94String_intern_pool
For more info on String intern/Literal Pool.
Last one in this case :
Now another interesting case: int i = 0; byte b = 0;
Here “==” and Equals() is as: i == b >> True
i.Equals(b) >> False
So, it means Equals method compare not only value but it compares TYPE also in Value Types.
Here i and b are 2 different types.
when i.equals(b) is called, it cannot be determined which equals is to be called
one for i type's or b type's so, default equals methods is called
which cannot recognise values in i and b, hence returns false, but that does
not make "equals()" method suitable for reference identity.
Thanks and Regards,
Spydaios
http://spydaios.blogspot.com ________________________________________
He is able who thinks he is able.
- Lord Buddha
elias_kuet
Member
14 Points
13 Posts
What is the difference between a.Equals(b) and a == b?
Jan 07, 2010 08:38 AM|LINK
What is the difference between a.Equals(b) and a == b?
For value types: “==” and Equals() works same way : Compare two objects by VALUE
Example:
int i = 5;
int k= 5;
Here “==” and Equals() is as:
i == k >> True
i.Equals(k) >> True
For reference types : both works differently :
“==” compares reference – returns true if and only if both references point to the SAME object while
"Equals" method compares object by VALUE and it will return true if the references refers object which are equivalent
Example:
StringBuilder objSb1 = new StringBuilder("Elias");
StringBuilder objSb2 = new StringBuilder("Elias");
Here “==” and Equals() is as:
objSb1 == objSb2 >> False
objSb1.Equals(objSb2) >> True
But now look at following issue:
string s1 = "Elias";
string s2 = "Elias";
Here “==” and Equals() is as:
In above case the results will be,
s1 == s2 >> True
s1.Equals(s2) >> True
String is actually reference type: its a sequence of “Char” and its also immutable but as you saw above, it will behave like Value Types in this case.
Exceptions are always there ;)
Now another interesting case:
int i = 0;
byte b = 0;
Here “==” and Equals() is as:
i == b >> True
i.Equals(b) >> False
So, it means Equals method compare not only value but it compares TYPE also in Value Types.
Recommendation:
For value types: use “==”
For reference types: use Equals method.
MCP, MCTS(WEB)
Software Engineer
Rohit Rao
Participant
1869 Points
357 Posts
Re: What is the difference between a.Equals(b) and a == b?
Jan 07, 2010 11:16 AM|LINK
a.Equals(b) - > it is used for comparision between two objects.
a==b - > it is used to Compare the refrences of two objects.
e.g
protected void Page_Load(object sender, EventArgs e)
{
string sStr1 = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
string sStr2 = new string(new char[] { 'h', 'e', 'l', 'l', 'o' });
Response.Write(sStr1 == sStr2);
Response.Write(sStr1.Equals(sStr2));
// now the output is - true true //Now make some changes here object obj1 = sStr1;
object obj2 = sStr2;
Response.Write(obj1 == obj2);
Response.Write(obj1.Equals(obj2));
//here the output is true true false true
}
Note - > obj1==obj2 is false because compiler doesnot know the contents of obj1 & obj2.So they are references to different - 2 strings, so the operators returns false.
Hope it helps you.
Mark as Answer if it helps you.
Rohit Rao
}
elias_kuet
Member
14 Points
13 Posts
Re: What is the difference between a.Equals(b) and a == b?
Jan 07, 2010 11:28 AM|LINK
Yes, I'm saying what you said, thanks
MCP, MCTS(WEB)
Software Engineer
Spydaios
Member
209 Points
107 Posts
Re: What is the difference between a.Equals(b) and a == b?
Jan 11, 2010 07:55 AM|LINK
Well,for any object (obj1 == obj2) is true if
ObjectType obj1 = ~SomeValue~;
ObjectType obj2 = obj1;
OR
ObjectType obj1 = Obj#;
ObjectType obj2 = Obj#; WHERE Obj# is of type ObjectType
Coz, all are same references.
And (obj1==obj2) is false otherwise.
Because each object has different hashcode.
i.e. each object has different hashcode, if two object are same that means they have same hashcode
refer : http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx
and http://msdn.microsoft.com/en-us/library/system.object.gethashcode%28VS.71%29.aspx
Well, when you do obj1.equal(obj2) you are using it for value comparision and not for
reference identity.
But now look at following issue: string s1 = "Elias"; string s2 = "Elias"; Here “==” and Equals() is as: In above case the results will be, s1 == s2 >> True s1.Equals(s2) >> TrueBut here, A String object is called immutable (read-only) because its value cannot be modified once it has been created. Methods that appear to modify a String object actually return a new String object that contains the modification.
There is something called "String literal pool".
When you make a string object say Str1 = "123"
it goes in this pool.
Now you prepare second object as Str2 = "123"
new object might not get prepared as it already exist in pool
Instead it points to same reference.
So,
Another thing StringBuilder is Mutable object i.e
if you do
StringBuilder StBlObj = new StringBuilder();
StBlObj = "123";
StBlObj = "123245";
Same object gets updated , no new object is created.
Please refer to this link : http://en.csharp-online.net/CSharp_String_Theory%E2%80%94String_intern_pool
For more info on String intern/Literal Pool.
Last one in this case :
Here i and b are 2 different types.
when i.equals(b) is called, it cannot be determined which equals is to be called
one for i type's or b type's so, default equals methods is called
which cannot recognise values in i and b, hence returns false, but that does
not make "equals()" method suitable for reference identity.
Spydaios
http://spydaios.blogspot.com
________________________________________
He is able who thinks he is able.
- Lord Buddha
monalisa_pra...
Member
4 Points
3 Posts
Re: What is the difference between a.Equals(b) and a == b?
Jan 11, 2010 12:16 PM|LINK
a==b is used to compare references where as a.Equals(b) is used to compare the values they are having.
For Ex:
public class Class1
{
string name = String.Empty;
public Class1(string strName)
{
strName = name;
}
}
Class1 a = new Class1("Hello");
Class1 b = new Class1("Hello");
a==b // Returns false
a.Equals(b) // Returns true
asp.net developers india
Monalisa
Mindfire: India's Only Company to be both Apple Premier & Microsoft Gold certified.
inniharjena
Member
36 Points
14 Posts
Re: What is the difference between a.Equals(b) and a == b?
Jan 19, 2010 05:51 AM|LINK
Hi,
Both "==" and ".Equals()" are used to compare value in case of value types and reference in case of reference types.
The advantage provided by ".Equals()" over "==" is that it allows the user to override the ".Equals()" method for your class.
rami_nassar
Contributor
3608 Points
828 Posts
Re: What is the difference between a.Equals(b) and a == b?
Feb 07, 2010 05:21 AM|LINK
you can't "==" to compare two objects, and that is the uses of .Equal Method
Nassar, Rami (MCP, MCTS, MCPD)
My Blog || E-Mail
Don't forget to click "Mark as Answer" on the post that helped you.
Matt-dot-net
Contributor
5262 Points
989 Posts
Re: What is the difference between a.Equals(b) and a == b?
Feb 07, 2010 05:26 AM|LINK
Ah, not so fast rami_nassar
public class MyObject { private Int32 aInt; private String aString; public static bool operator ==(MyObject a1, MyObject a2) { if (a1.aInt == a2.aInt && a1.aString == a2.aString) return true; return false; } public static bool operator !=(MyObject a1, MyObject a2) { if (a1.aInt != a2.aInt || a1.aString != a2.aString) return true; return false; } }srinivas3303...
Member
8 Points
6 Posts
Re: What is the difference between a.Equals(b) and a == b?
Jun 18, 2010 05:51 AM|LINK
Yeah nice to know the difference,. Can u tell me which shd be used when?