C# Enum Flags Comparison

前端 未结 5 902
心在旅途
心在旅途 2021-02-09 10:03

Given the following flags,

  [Flags]
    public enum Operations
    {
        add = 1,
        subtract = 2,
        multiply = 4,
        divide = 8,
        ev         


        
5条回答
  •  忘了有多久
    2021-02-09 10:58

    Try this:

       public double Evaluate(double input)
    {
        if ((operation & (Operations.add | Operations.eval)) == (Operations.add | Operations.eval))
            currentResult += input;
        else if ((operation & (Operations.subtract | Operations.eval)) == (Operations.subtract | Operations.eval))
            currentResult -= input;
        else
            currentResult = input;
    
        operation = null;
    
        return currentResult;
    }
    

提交回复
热议问题