Search code examples
c#.netstringglobalizationcultureinfo

How to make string comparisons to be case insensitive on process / thread level?


I'm reading about string comparison in C# and I wondered:

Can I predefine the compare info on a process / thread level to be case insensitive so I can use == directly when compare two strings?


Solution

  • No, you can't.

    The == operator calls string.Equals, which on it's own, calls string.EqualsHelper.

    As you can see, it doesn't use any culture or comparision settings (like the Equals(String value, StringComparison comparisonType) overload does). It just compares the string character by character.

    You have to call an overload of Equals to get the result you want, which isn't the default behavior, and can't be changed, unless you have a way to override every string with an own implementation of string.Equals or the == operator.