1.

Difference between the Equality Operator (==) and Equals() Method in C#?

Answer»

Although both are used to compare two OBJECTS by value, still they both are used differently. 

For ex.:

int x = 10;int y = 10;Console.WriteLine( x == y);Console.WriteLine(x.Equals(y));OUTPUT:TrueTrue

Equality operator (==) is a reference type which means that if equality operator is used, it will return TRUE only if both the references point to the same object.  

Equals() method: Equals method is used to compare the values CARRIED by the objects. int x=10, int y=10. If x==y is compared then, the values carried by x and y are compared which is equal and therefore they return true. 

Equality operator: Compares by reference

Equals(): Compares by value 



Discussion

No Comment Found