C# how to use enum with switch

前端 未结 11 432
小鲜肉
小鲜肉 2020-12-08 06:04

I can\'t figure out how to use switches in combination with an enum. Could you please tell me what I\'m doing wrong, and how to fix it? I have to use an enum to make a basic

相关标签:
11条回答
  • 2020-12-08 06:24

    Your code is fine. In case you're not sure how to use Calculate function, try

    Calculate(5,5,(Operator)0); //this will add 5,5
    Calculate(5,5,Operator.PLUS);// alternate
    

    Default enum values start from 0 and increase by one for following elements, until you assign different values. Also you can do :

    public enum Operator{PLUS=21,MINUS=345,MULTIPLY=98,DIVIDE=100};
    
    0 讨论(0)
  • 2020-12-08 06:25

    Since C# 8.0 introduced a new switch expression for enums you can do it even more elegant:

    public double Calculate(int left, int right, Operator op) =>
                op switch 
            {
                Operator.PLUS => left + right,
                Operator.MINUS => left - right,
                Operator.MULTIPLY => left * right,
                Operator.DIVIDE => left / right,
                _    =>  0
            }
    

    Ref. https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8

    0 讨论(0)
  • 2020-12-08 06:25

    You should not cast to integer. And for the division, you need to cast left to double first, if not you will be doing an integer divide.

    public enum Operator
    {
        PLUS, MINUS, MULTIPLY, DIVIDE
    }
    
    public double Calculate(int left, int right, Operator op)
    {
        double sum = 0.0;
    
        switch(op)
        {
           case Operator.PLUS:
           sum = left + right;
           return sum;
    
           case Operator.MINUS:
           sum = left - right;
           return sum;
    
           case Operator.MULTIPLY:
           sum = left * right;
           return sum;
    
           case Operator.DIVIDE:
           sum = (double)left / right;
           return sum;
    
           default:
           return sum;
       }
    
       return sum;
    }
    
    0 讨论(0)
  • 2020-12-08 06:28

    No need to convert. You can apply conditions on Enums inside a switch. Like so,

    public enum Operator
    { 
        PLUS,
        MINUS,
        MULTIPLY,
        DIVIDE
    }
    
    public double Calculate(int left, int right, Operator op)
    {
        switch (op)
        {
            case Operator.PLUS: return left + right; 
            case Operator.MINUS: return left - right; 
            case Operator.MULTIPLY: return left * right;
            case Operator.DIVIDE: return left / right;
            default: return 0.0; 
        }
    }
    

    Then, call it like this:

    Console.WriteLine("The sum of 5 and 5 is " + Calculate(5, 5, Operator.PLUS));
    
    0 讨论(0)
  • 2020-12-08 06:29

    In case you don't want to use return statement for each case, try this:

    Calculate(int left, int right, Operator op)
    {
       int result = 0;
       switch(op)
       {
            case Operator.PLUS:
            {
                result = left + right;;  
            }
            break;
            ....
       }
    
       return result;
    }
    
    0 讨论(0)
  • 2020-12-08 06:30

    All the other answers are correct, but you also need to call your method correctly:

    Calculate(5, 5, Operator.PLUS))
    

    And since you use int for left and right, the result will be int as well (3/2 will result in 1). you could cast to double before calculating the result or modify your parameters to accept double

    0 讨论(0)
提交回复
热议问题