I'm attempting to implement a calculator-like application in C#. But the way I've currently implemented it seems a little messy and inefficient. It works, but it's just I'm pretty sure it can be achieved in a more elegant solution.I looked to see if I could factor out any repeating code, such as the switch statement - but without having numerous parameters in the method, I don't think it'll make things much better.Here's the main portion of the code:private double Calculate(){    double answer = 0.0;    foreach (double number in this.numbers)    {        int index = this.numbers.IndexOf(number);        if (index != this.numbers.Count() - 1)        {            if (answer != 0.0)            {                switch ((int)operators[index])                {                    case (int)Operator.Plus:                        answer = (double)(answer + numbers[index + 1]);                        break;                    case (int)Operator.Minus:                        answer = (double)answer - numbers[index + 1];                        break;                    case (int)Operator.Divide:                        answer = (double)answer / numbers[index + 1];                        break;                    case (int)Operator.Multiply:                        answer = (double)answer * numbers[index + 1];                        break;                }            }            else            {                switch ((int)operators[index])                {                    case (int)Operator.Plus:                        answer += (double)(number + numbers[index + 1]);                        break;                    case (int)Operator.Minus:                        answer += (double)number - numbers[index + 1];                        break;                    case (int)Operator.Divide:                        answer += (double)number / numbers[index + 1];                        break;                    case (int)Operator.Multiply:                        answer += (double)number * numbers[index + 1];                        break;                }            }        }           }    return answer;}Just for reference, Operators is an ENUM that's defined as:private enum Operator{    Plus = 1,    Minus = 2,    Multiply = 3,    Divide = 4,}and the two lists:private List<double> numbers;private List<Operator> operators;Any ideas on how I could improve this?
Calculator-like application
c#;calculator
Your first big problem is going to be fighting your design.  Multiple case statements like that are asking to be removed either due to polymorphism, or stuck deep in the bowls of some sort of AbstractFactory, and only referred to once (sorry, I've just been going the Clean Code book).In this case, making this setup polymorphic is going to be your friend...  Otherwise, you're going to have an interesting time later, trying to add, say modulous (%).To start with, define an Operation interface.  Something like this:  public interface Operation {    public double Operate(double argA, double argB);}You then need to implement each operation seperately:public struct Division : Operation {    // For obvious reasons, the proper argument names should be specified    public double Operate(double quotient, double divisor) {        return quotient / divisor;    }}This will allow you to add any number of future operations quite simply (including some really complicated stuff) - so long as it only takes 2 arguments... (note - I'm attempting to retain the general concept of your design; there are other possibilities here).  And replace the List<Operator> with a List<Operation>.Your calculate method then becomes, simply:  private double Calculate () {    double answer = 0.0;    for(int i = 0; i < numbers.Count(), i < operators.Count(), i++) {        // Of course, this doesn't check for divide-by-zero...        answer = operators[i].Operate(answer, numbers[i]);     }    return answer; }Your current method had the following issues - Listing multiple copies of the same number, but with different operations, would re-use only one of the operations (probably the first in the list).You aren't sufficiently checking for array out-of-bounds errors -  if numbers is longer than operators, you'll run out of operators (it's debatable if ignoring numbers on the end is better, as I do...).Your if (answer == 0.0) statement really encapsulates the exact same logic.  You just need to look at it a little different.