Evaluate Expressions in Switch Statements in C#

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

    The only way I could think of (and I really don't recommand it), would be as follows:

    int someValue;
    
    switch (Math.Max(someValue, -1))
    {
        case -1:
            // will be executed for everything lower than zero.
            break;
    
        case 0:
           // will be executed for value 0.
           break;
    
        case 1:
           // will be executed for value 1.
           break;
    
        default:
           // will be executed for anything else.
           break;
    }
    

提交回复
热议问题