哪个字符串比较器与 switch 语句一起使用?

Which string comparer is used with switch statements?

执行 switch 语句时如何比较字符串?线程/计算机的当前文化是否影响 switch 评估?我养成了在比较字符串时总是指定比较器的习惯,所以如果能确认这一点就好了。

我怀疑是 StringComparer.Ordinal,但我找不到任何相关文档。

Does the current culture of the thread / computer affect switch evaluation?

不,不是。

switch,在幕后使用 Equals。因此它是有序的:

This method performs an ordinal (case-sensitive and culture-insensitive) comparison.

我们怎么知道 switch 使用了 Equals?嗯the docs状态:

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.

后一个要点适用于此。