public static void Main(string[] args) { Calculator c1 = new Calculator(); //Instantiate Delegate Object CalculatorDelegate cd1 = new CalculatorDelegate(c1.Add); //Multi cast Delegate cd1 += c1.Sub; //Invoke var x = cd1.Invoke(10, 10); Console.WriteLine(x); }
//Method called when the equals our operation button is called public string ProcessResult() { /* * Console.WriteLine("op2 " + op2); * Console.WriteLine("op1 " + op1); * * */ int len = Operands.ToArray().Length; if (len == 2) { //process the operation with two operand string op2 = Operands.Pop(); string op1 = Operands.Pop(); if (CalculatorOperator == null) { //when result is called without setting the operation return(op2); } else { String result; if (op2.Length == 0) { if (op1.Length == 0) { //when result is called when a operation was set but there are no operands return(op1); } //when there is only the first operand result = CalculatorOperator.Invoke(op1, op1); } else { //when there are both operands and the process is regular result = CalculatorOperator.Invoke(op1, op2); } Operands.Push(result); return(result); } } else if (len == 1) { //process the operation with only one operand string op1 = Operands.Pop(); if (CalculatorOperator == null) { //when there is no operation set return(op1); } else { String result; if (op1.Length == 0) { //when the operand does not have value result = op1; } else { //Regular process result = CalculatorOperator.Invoke(op1, null); } Operands.Push(result); return(result); } } else //if (len == 0) { //Will never be called: the first operand is always set, if not here we are setting it return(NextOperand()); } }