Monday, June 18, 2007

Difference between "==" and "object.equalsto"

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

//do something

//above code will compile successfully and internally the int b will convert to object type and compare

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

NeoTechie said...

Great post.
Keep up the good work.

Most Recent Post

Community Updates

Subscribe Blog via Email

Enter your email address:

Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape