Evaluate Expressions in Switch Statements in C#

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

    You could do something like this at the end of your switch statement:

    default:
        if(num < 0)
        {
            ... // Code
        }
        break;
    
    0 讨论(0)
  • 2021-01-31 02:07

    I know that this topic is pretty old but if someone still looking for the answer now in C# 7 it's possible. Here is an example:

    switch (value)
    {
         case var expression when value < 0:
             //some code
             break; 
    
         case var expression when (value >= 0 && value < 5):
             //some code
             break;
    
         default:
             //some code
             break;
    }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-01-31 02:09

    you can do this

    switch (mark)
    {
        case int n when n >= 80:
            Console.WriteLine("Grade is A");
            break;
    
        case int n when n >= 60:
            Console.WriteLine("Grade is B");
            break;
    
        case int n when n >= 40:
            Console.WriteLine("Grade is C");
            break;
    
        default:
            Console.WriteLine("Grade is D");
            break;
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-31 02:12

    I've run into the following pattern recently, and while I abhor it, I can't argue that it's not practical:

    switch(0)
    {
      case 0 when x < 0:
        ...
        break;
      case 0 when a > 5 && x == 0:
        ...
        break;
    }
    

    The use of dummy expressions ((0) in the switch, and case 0) is absolutely terrible, and I'd hate for it to become an idiom, but hey - it's concise and very clear. For sure it's not some hack that completely obscures the meaning and needs arcane knowledge, but C# would do well to obviate the need for it. I'd like the following to be legal:

    switch                   // maybe () here, if the grammar would demand
    {
      case when x < 0:       // I like the case to stay
        ...
      case when a > 5 && x == 0:
        ...
    }
    
    0 讨论(0)
提交回复
热议问题