What is the difference between "==" and "object.EqualsTo"
"==" Vs "Object.EqualsTo"
'==' only compares the value where as 'object.equalsto' compares the both value as well as type
Both are used for comparison and both returns the boolean value (true/false)
Case 1. In case a and b both are different datatype then also a.Equals(b) can be used to compare but incase of == we cant even compile the code if a and b are different data type
Example1 for "==" Vs "Object.EqualsTo":
int a=0;
string b="o";
if(a.Equals(b))
{
//do something
}
//above code will compile successfully and internally the int b will convert to object type and compare
if(a==b)
{
//do something
}
//above code will give you the compilation error
Case 2. by using == we cant compare two object but Equals method will able to compare both the object internally
Example 2 for "==" Vs "Object.EqualsTo":
a==b is used to compare references where as a.Equals(b) is used to compare the values they are having.
for e.g
class Mycar
{
string colour;
Mycar(string str)
{
colour = str;
}
}
Mycar a = new Mycar("blue");
Mycar b = new Mycar("blue");
a==b // Returns false
a.Equals(b) // Returns true
1 comment:
Great post.
Keep up the good work.
Post a Comment