c# - C# 8 新 switch 可以替换包含多个的代码块吗? : ? : expressions?

标签 c# switch-statement c#-8.0

这是我现在正在做的一个示例:

return
    shpc == 0 ? "Currently, based on your selection below, you have not yet identified any hidden cards in your card deck." :
    shpc == 1 ? "Currently, based on your selection below, you have one hidden card in your card deck." :
                $"Currently, based on your selection below, you have {shpc} hidden cards in your card deck. These will not be visible.";

代码字,但不太了解添加到 switch 的内容我想知道这是否也可以使用 switch 表达式来完成?

最佳答案

试试这个

return shpc switch 
{
    0 => "Currently, based on your selection below, you have not yet identified any hidden cards in your card deck.",
    1 => "Currently, based on your selection below, you have one hidden card in your card deck.",
    _ => $"Currently, based on your selection below, you have {shpc} hidden cards in your card deck. These will not be visible."
};

关于c# - C# 8 新 switch 可以替换包含多个的代码块吗? : ? : expressions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59510149/

相关文章:

c# - 如何在 List<object> 中搜索具有其字段值的对象

c# - 创建一个set get类并在构造函数中传递参数

c# - C#'s can' t 使 `notnull` 类型可以为空

c# - 我可以在不复制 C# 8 中的元素的情况下遍历结构数组吗?

java - 对于 Android 事件,为什么 switch 语句比 if-else 链更常见?

c# - 将 C# 8 可空引用类型标记为 "this can' t 为空”

c# - Rx 窗口、加入、GroupJoin?

c#:将一个int转换成它适合的最小字节数组

pointers - 'myVariable.(type)' 究竟返回什么?

c - 扫描 char 并将 Switch 语句与 If 语句组合?开关(时间)