在执行 switch
语句时如何比较字符串?线程/计算机的当前文化是否影响switch
评估?我养成了在比较字符串时总是指定比较器的习惯,所以确认这一点会很棒。
我怀疑它是 StringComparer.Ordinal
,但我找不到任何相关文档。
最佳答案
Does the current culture of the thread / computer affect switch evaluation?
不,它没有。
开关
,使用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.
后一个要点适用于此。
关于c# - 哪个字符串比较器与 switch 语句一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57544979/