Evaluate Expressions in Switch Statements in C#

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

    If your num can't be less than zero:

    public int GetSwitch(int num) { return num < 0 ? -1 : num; }
    switch(GetSwitch(num))
    {
    case 4: // some code ; break;
    case 3:// some code ; break;
    case 0: // some code ; break;
    case -1 :// some code ; break;
    }
    

    If it can, use some other "non-existent" number such as int.MinValue.

提交回复
热议问题