C# how to use enum with switch

前端 未结 11 465
小鲜肉
小鲜肉 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: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);
                }
            }
        }
    

提交回复
热议问题