Evaluate Expressions in Switch Statements in C#

前端 未结 12 461
抹茶落季
抹茶落季 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:11

    You cannot use comparisons in switches like you could in VB, you have 2 options here, replace the value you switch on with a known value and use that or - if you mean all other cases - you can use the default clause:

    switch(num)
    {
      case 4:
        // some code ;
        break;
      case 3:
        // some code ;
        break;
      case 0:
        // some code ;
        break;
      default:
        // some code ;
        break;
    }
    

    Note that this does not exactly like you asked for: any values other than 0,3,4 will end up in the deafult: clause.

提交回复
热议问题