开发者

String comparison : operator==() vs. Equals() [duplicate]

开发者 https://www.devze.com 2023-02-05 19:50 出处:网络
This question开发者_如何学运维 already has answers here: Closed 12 years ago. Possible Duplicate:
This question开发者_如何学运维 already has answers here: Closed 12 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号