15

On this page, a commenter writes:

Do NOT ever use .ToUpper to insure comparing strings is case-insensitive.

Instead of this:

type.Name.ToUpper() == (controllerName.ToUpper() + "Controller".ToUpper())) 

Do this:

type.Name.Equals(controllerName + "Controller", 
     StringComparison.InvariantCultureIgnoreCase)

Why is this way preferred?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501

2 Answers2

18

Here is the answer in details .. The Turkey Test (read section 3)

As discussed by lots and lots of people, the "I" in Turkish behaves differently than in most languages. Per the Unicode standard, our lowercase "i" becomes "İ" (U+0130 "Latin Capital Letter I With Dot Above") when it moves to uppercase. Similarly, our uppercase "I" becomes "ı" (U+0131 "Latin Small Letter Dotless I") when it moves to lowercase.

Fix: Again, use an ordinal (raw byte) comparer, or invariant culture for comparisons unless you absolutely need culturally based linguistic comparisons (which give you uppercase I's with dots in Turkey)

And according to Microsoft you should not even be using the Invariant... but the Ordinal... (New Recommendations for Using Strings in Microsoft .NET 2.0)

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
  • 5
    Thanks...I was suspecting something like this. It's irritating when people say you MUST do something a certain way, but they don't say WHY. – Robert Harvey Feb 13 '10 at 04:40
  • .. not fond of dictatorships either .. lol .. (*added the microsoft recomendations link*) – Gabriele Petrioli Feb 13 '10 at 04:46
  • 1
    I have also seen a small performance improvement with doing a case-insensitive compare vs. converting to upper/lowercase, so using the comparison flag is definitely the way to go... – Chris R. Donnelly Nov 26 '10 at 04:08
  • This is funny because for a long time Microsoft recommended using ToUpper instead of ToLower for this exact reason. It's always hard for me to find it but it's on the MSDN site. ... Or maybe it's not anymore. edit: Found it! https://msdn.microsoft.com/en-us/library/bb386042.aspx. Can anyone explain? – Brent Rittenhouse Sep 06 '17 at 17:51
9

In short, it's optimized by the CLR (less memory as well).

Further, uppercase comparison is more optimized than ToLower(), if that tiny degree of performance matters.

In response to your example there is a faster way yet:

String.Equals(type.Name, controllerName + "Controller", 
              StringComparison.InvariantCultureIgnoreCase);
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • There's no considerations about weirdness like umlauts, or anything like that? So this guy was just being a dictator about it? – Robert Harvey Feb 13 '10 at 04:34
  • @Robert - There are fringe benefits, like free null checking you don't have to worry about, but mainly it's an optimization difference...strings are **highly** optimized by the CLR, so taking advantages of the CLR shortcuts built in can dramatically increase performance if you're hitting that code in a heavy loop. Also, it'll reduce memory overhead as well. – Nick Craver Feb 13 '10 at 04:37