问题描述
我正在将 switch statement
转换为 switch expression
,但我没有这样做。我的函数哪里出错了?
解决方法
- 编写您的 switch 语句
- 将光标放在
switch
关键字上,然后按 Ctrl+。触发“快速操作和重构”菜单。 - 选择将 switch 语句转换为表达式。
快乐编码!
,or operator in switch 在 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文章