Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to compare C# string with '=='?

Tags:

string

c#

.net

Is it a good practice to compare strings with ==? Is there an equivalent to s1 == s2 in terms of Compare and Equals methods on string. If one uses those methods, without specifying CultureInfo FxCop will give a warning, is that a real problem?

like image 245
Lincoln Avatar asked Nov 18 '11 19:11

Lincoln


1 Answers

The == Operator is an ordinal, culture unaware string compare. Its using the same internal call as .Equals, and is just fine for the "usual" string compare stuff.

If you need Culture-Aware comparings (eg. For GUI purposes ) , like german double-s or ß, use

CultureInfo ci = new CultureInfo("de-DE");
String.Compare("Strasse", "Straße", true, ci)
like image 70
Alex Avatar answered Oct 15 '22 09:10

Alex