【问题标题】:Multiple case in c# 8.0 switch expressionc# 8.0 switch 表达式中的多个案例
【发布时间】:2021-06-29 05:12:48
【问题描述】:

我正在将switch statement 转换为switch expression,但我不这样做。我的函数哪里出错了?

【问题讨论】:

  • 欢迎来到 Stack Overflow。请不要粘贴代码的图像,而是复制粘贴内联代码并格式化。

标签: c#-8.0


【解决方案1】:

or 运算符在 c# 8.0 中不可用,在 c# 9.0 或更高版本中可用,在 c# 8.0 中您可以解决如下:

static string CardinalToOrdinal(int number)
        {
            return number switch
            {
                11 => $"{ number}th",
                12 => $"{ number}th",
                13 => $"{ number}th",
                _ => $"{number}" + number.ToString()[number.ToString().Length - 1]
                switch
                {
                    '1' => "st",
                    '2' => "nd",
                    _ => "th"
                }
            };
        }

欲了解更多信息,请查看this文章

【讨论】:

  • 哦,是的,非常感谢。
【解决方案2】:
  1. 编写你的 switch 语句
  2. 将光标放在switch 关键字上,然后按Ctrl+。触发快速操作和重构菜单。
  3. 选择将 switch 语句转换为表达式。

Microsoft Docs

编码愉快!

【讨论】:

  • Sr.I 运行 c# 8.0 Visual Studio 代码。所以,我不是 Ctrl +。你知道不同的解决方案
猜你喜欢
  • 2019-08-25
  • 2019-11-17
  • 1970-01-01
  • 2020-03-14
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多