C # uses delegation, interfaces, anonymous methods, and generic delegation to implement addition, subtraction, multiplication, division,
Using C # To implement addition, subtraction, multiplication, division, and Division algorithms is often used as a beginner's exercise. This document describes how to implement delegation, interface, anonymous method, and generic delegation.
Delegate implementation
The addition, subtraction, multiplication, and Division have the same number of parameters, type, and return type. First, we thought of using the delegate implementation.
// Create a delegate public delegate decimal MathOperation (decimal left, decimal right); // create method parameters and return results in line with the delegate definition public static decimal Add (decimal left, decimal right) {return left + right;} public static decimal Subtract (decimal left, decimal right) {return left-right;} public static decimal Multiply (decimal left, decimal right) {return left * right;} public static decimal Divide (decimal left, decimal right) {return left/right;} // return the delegate type private static MathOperation GetOperation (char evaluate) {switch (condition) {case '+': return Add; case '-': return Subtract; case '*': return Multiply; case '/': return Divide ;} throw new NotSupportedException ("");} // encapsulate a method to take the operands and symbols into account, and return the private static decimal Eval (string expr) {var elements = expr. split (new [] {''}, 3); var left = Decimal. parse (elements [0]); var right = Decimal. parse (elements [1]); var ope = elements [2] [0]; return GetOperation (op) (left, right);} void Main () {Console. writeLine (Eval ("1 3 + "));}
Interface implementation
Above, the delegate is used at the method level. At the class level, interfaces can be used to encapsulate the commonalities of addition, subtraction, multiplication, and division.
Public interface IMathOperation {decimal Compute (decimal left, decimal right);} public class AddOperation: IMathOperation {decimal Compute (decimal left, decimal right) {return left + right ;}} public class SubtractOperation: IMathOperation {decimal Compute (decimal left, decimal right) {return left-right ;}} public class MultiplyOperation: IMathOperation {decimal Compute (decimal left, decimal right) {return left * right;} public class DivideOperation: IMathOperation {decimal Compute (decimal left, decimal right) {return left/right ;}} // obtain the interface type private static IMathOperation GetOperation (char operation) {switch (callback) {case '+': return new AddOperation (); case '-': return new SubtractOperation (); case '*': return new MultiplyOperation (); case '/': return new DivideOperation ();} throw new NotSupportedException ("");}...
Use the anonymous method
The delegate can also be used in combination with the anonymous method.
public delegate decimal MathOperation(decimal left, decimal right);private static MathOperation GetOperation(char oper){ switch(oper) { case '+': return delgate(decimal left, decimal right) {return left + right;}; case '-': return delgate(decimal left, decimal right) {return left - right;}; case '*': return delgate(decimal left, decimal right) {return left * right;}; case '/': return delgate(decimal left, decimal right) {return left / right;}; } throw new NotSupportedException("");}
Generic Delegation
Generic delegation is more concise.
private static Func<decimal, decimal, decimal> GetOperation(char oper){ switch(oper) { case '+': return (left, right) => left + right; case '-': return (left, right) => left - right; case '*': return (left, right) => left * right; case '/': return (left, right) => left / right; } throw new NotSupportedExcepton("");}
For now.