DEV Community

Discussion on: Don't use String.ToLower() in C# when comparing strings

Collapse
 
ezekiel_juel_83edd99bfb8c profile image
Ezekiel Juel

Do you have a similar trick for switch statements to ignore case?

Collapse
 
davidkroell profile image
David Kröll

I'm not sure if this works. I'm not that familiar with how switch statements actually compare the things.
In fact, during lowering (a step in compilation) the switch is transformed to an nested if, in some cases to a binary search tree to optimize for efficiency.

Collapse
 
smartmanapps profile image
SmartmanApps

From what I've seen switch only works with actual strings - an example of something that DOES work is nameof(thing), but that's because the result is a string - so I always just use ToLower() as the argument I'm passing into a switch. The other option is if you KNOW where the caps might be, just create a fall-through for it...

case "ThisThing":
case "thisthing":
dostuff;
break;