Multi-variable switch statement in C#

后端 未结 12 512
-上瘾入骨i
-上瘾入骨i 2020-12-15 16:54

I would like use a switch statement which takes several variables and looks like this:

switch (intVal1, strVal2, boolVal3)
{
   case 1, \"hello\", false:
            


        
12条回答
  •  北荒
    北荒 (楼主)
    2020-12-15 17:33

    You can do this in C# 7 and higher with the when keyword:

    switch (intVal1)
    {
        case 1 when strVal2 == "hello" && boolVal3 == false:
            break;
        case 2 when strVal2 == "world" && boolVal3 == false:
            break;
        case 2 when strVal2 == "hello" && boolVal3 == false:
            break;
    }
    

提交回复
热议问题