String comparison : operator==() vs. Equals() [duplicate]
Possible Duplicate:
C#: Are string.Equals() and == operator really same?
For string comparison, which approach is better (and safe):
string s1="Sarfaraz";
string s2="Nawaz";
bool result1 = (s1==s2) ;//approach 1
bool result2 = s1.Equals(s2) ;//approach 2
Or both are same under the hood?
I like Equals()
because the available StringComparison
option is very useful.
The ==
and !=
operators are based on the value, so they are safe to use, even though String
is a reference type.
精彩评论