Difference between a.Equals(b) and a == b

Difference between a.Equals(b) and a == b

a.Equals(b) or a == b ?

In .Net Framework data types can be classified according to whether a variable of a particular type stores its own data or a pointer to the data. If the variable stores its own data, it is a value type and if it holds a pointer to data elsewhere in memory it is a reference type. You can assign either a reference type or a value type to a variable of the Object data type

The Equals() and == operator are used for comparison and both returns the boolean value (true/false).

Value Type


For Value Type == and Equals() works in the same way, both compare two object by value.

a==b and a.Equals(b) returns true , because in this case both compare two object by value.

Reference Type


In case of Reference Type both works in different way.


sb1 == sb2 returns false and sb1.Equals(sb2) returns true.

== operator compares reference returns true when both references point to the same object and Equals() compares object by value and it will return true if the references refers object which are equivalent.

Comments

Popular Posts