switch语法 - zLulus/My_Note GitHub Wiki

一般使用

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