Evaluate Expressions in Switch Statements in C#

前端 未结 12 462
抹茶落季
抹茶落季 2021-01-31 01:42

I have to implement the following in a switch statement:

switch(num)
{
  case 4:
    // some code ;
    break;
  case 3:
    // some code ;
    brea         


        
12条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 02:13

    Note: the answer below was written in 2009. Switch patterns were introduced in C# 7.


    You can't - switch/case is only for individual values. If you want to specify conditions, you need an "if":

    if (num < 0)
    {
        ...
    }
    else
    {
        switch(num)
        {
            case 0: // Code
            case 1: // Code
            case 2: // Code
            ...
        }
    }
    

提交回复
热议问题