When doing a code review I often see code comparing two strings where the developer wants the comparison to be case insensitive and chose to use ToLower()
or ToUpper()
and ==
to get the job done. However because the dot net framework handles different cultures just using ToUpper/Lower could produce unpredictable results depending on the config of the machine your code is running on. Instead you should use the OrdinalIgnoreCase option on string.Equals
or StringComparer/StringComparison
So instead of:
if(MyString.ToUpper() == YourString.ToUpper()) { ... }
You should do this:
if(MyString.Equals(YourString, StringComparison.OrdinalIgnoreCase)) { ... }
MSDN also has a decent example.