C # uses delegation, interfaces, anonymous methods, and generic delegation to implement addition, subtraction, multiplication, division,

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.