How are strings compared when doing switch
statements? Does the current culture of the thread / computer affect switch
evaluation? I got in the habit of always specifying a comparer when comparing strings, so it would be great to have this confirmed.
I suspect it's StringComparer.Ordinal
, but I cannot find any documentation on this.
Does the current culture of the thread / computer affect switch evaluation?
No, it does not.
switch
, uses Equals
under the covers. Thus it is ordinal:
This method performs an ordinal (case-sensitive and culture-insensitive) comparison.
How do we know switch
uses Equals
? Well the docs state:
The constant expression is evaluated as follows:
If expr and constant are integral types, the C# equality operator determines whether the expression returns true (that is, whether expr == constant).
Otherwise, the value of the expression is determined by a call to the static Object.Equals(expr, constant) method.
The latter bullet point is what applies here.