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. We 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("Test");
StringBuilder sb2 = new
StringBuilder("Test");
sb1 ==
sb2 returns false and sb1.Equals(sb2) returns true.
StringBuilder sb1 = new
StringBuilder("Test");
StringBuilder sb2 = sb1;
Both sb1
== sb2 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.

No comments:
Post a Comment