C# how to use enum with switch

前端 未结 11 433
小鲜肉
小鲜肉 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:31

    Two things. First, you need to qualify the enum reference in your test - rather than "PLUS", it should be "Operator.PLUS". Second, this code would be a lot more readable if you used the enum member names rather than their integral values in the switch statement. I've updated your code:

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

    Call this with:

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

    simply don't cast to int

     switch(operator)
        {
           case Operator.Plus:
           //todo
    
    0 讨论(0)
  • 2020-12-08 06:39

    The correct answer is already given, nevertheless here is the better way (than switch):

    private Dictionary<Operator, Func<int, int, double>> operators =
        new Dictionary<Operator, Func<int, int, double>>
        {
            { Operator.PLUS, ( a, b ) => a + b },
            { Operator.MINUS, ( a, b ) => a - b },
            { Operator.MULTIPLY, ( a, b ) => a * b },
            { Operator.DIVIDE ( a, b ) => (double)a / b },
        };
    
    public double Calculate( int left, int right, Operator op )
    {
        return operators.ContainsKey( op ) ? operators[ op ]( left, right ) : 0.0;
    }
    
    0 讨论(0)
  • 2020-12-08 06:49

    You don't need to convert it

    switch(op)
    {
         case Operator.PLUS:
         {
            // your code 
            // for plus operator
            break;
         }
         case Operator.MULTIPLY:
         {
            // your code 
            // for MULTIPLY operator
            break;
         }
         default: break;
    }
    

    By the way, use brackets

    0 讨论(0)
  • 2020-12-08 06:50
     public enum Operator
        {
            PLUS, MINUS, MULTIPLY, DIVIDE
        }
    
        public class Calc
        {
            public void Calculate(int left, int right, Operator op)
            {
    
                switch (op)
                {
                    case Operator.DIVIDE:
                        //Divide
                        break;
                    case Operator.MINUS:
                        //Minus
                        break;
                    case Operator.MULTIPLY:
                        //...
                        break;
                    case Operator.PLUS:
                        //;;
                        break;
                    default:
                        throw new InvalidOperationException("Couldn't process operation: " + op);
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题