Diff bet ween== and Equals ?

In our daily programming we have come across these expressions many times. here is when you should choose one over the other.Both these  operator perform the same function i.e comparing the equality but the way they do it depends on who is using them.
For value types both == and Equals mean the same ,they compare the value present in both the types.eg
                int a=5;int b=5;
Both these return True as they are comparing the value inside them and both the variables have same value.

For Reference types  ==  checks whether they are pointing to the same reference and equals checks if both the reference contains the same value.
StringBuilder s1 = new StringBuilder(“Name”);
StringBuilder s2 = new StringBuilder(“Name”);
Console.WriteLine(s1 == s2);
Will display:

No comments:

Post a Comment