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
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