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

标签 c# string switch-statement string-comparison cultureinfo

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

相关文章:

c# - 如何以编程方式切换样式表?

java - 将 int 传递给 Android 应用程序的 .xml

python - python `str()`函数是否调用类的 `__str__()`函数?

php - 使用字符串参数创建开关不起作用

c# - 进程需要按 <enter> 才能继续。可以使用 Mono GTK# 吗?

c# - C# 中的新手 MVVM 尝试使用 Esc 关闭应用程序(处理键命令)

Python:你如何准确地获取一个字符串,将其拆分、反转并重新组合在一起?

javascript - Javascript 下拉列表的大小写选择

C++ 变量在 "if"循环中丢失赋值

c# - AvalonEdit 更改代码中的语法突出显示