Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use '==' for .NET localized string comparisons?

What are the reasons not to use "==" to compare localized strings in .NET? How would the comparison execute in regards to the CultureInfo if I do use it?

like image 299
Peter K Avatar asked Nov 17 '11 23:11

Peter K


2 Answers

If you compare culture-aware strings with ==, for example "Strasse" with "Straße", it returns false.

If you need culture-aware comparings for UI stuff (Sorting of Listview), you use String.Compare with the related CultureInfo.

CultureInfo ci = new CultureInfo("de-DE");
String.Compare("Strasse", "Straße", true, ci) // Returns zero
like image 72
Alex Avatar answered Oct 13 '22 11:10

Alex


== is culture-insensitive - it's a simple ordinal comparison. So two strings which are culturally equal - or even equal in terms of other canonicalization forms - may not be equal via ==. It basically treats each string like a char array.

like image 30
Jon Skeet Avatar answered Oct 13 '22 10:10

Jon Skeet