gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:12:37 24 4
gpt4 key购买 nike

在执行 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com