Net-informations.com
SiteMap  | About    

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.

int a = 10; int b = 10;

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.

StringBuilder sb1 = new StringBuilder("Asp.Net"); StringBuilder sb2 = new StringBuilder("Asp.Net");

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.









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.