switch语法

一般使用

return a switch
{
    100 => 10,
    _ => 99,
};
return tag switch
{
    TagEnum.First => 1,
    TagEnum.Two => 2,
    TagEnum.Three => 3,
    _ => 0
};

使用属性

return tagObject switch
{
    { Tag1: TagEnum.First } => 1,
    { Tag1: TagEnum.Two } => 2,
    { Tag1: TagEnum.Three } => 3,
    _ => 0
};

使用位置

return tagObject switch
{
    var (x, y) when x == TagEnum.First && y == TagEnum.Two
        => 3,
    _ => 0
};

示例代码

SwitchTestDemo

posted @ 2022-03-07 20:45  Lulus  阅读(294)  评论(0编辑  收藏  举报