C # Delegate (function parameter transfer)

Source: Internet
Author: User

When I was learning to delegate this item, I didn't quite understand the function parameter. So I made a deep understanding of an example and shared it with you.

First, let's talk about my understanding of delegation. Delegation is a variable that can execute multiple similar functions. The reason why it is a variable that executes similar functions is that it can only accept one function, A function can only execute one type of function. Its function is to improve the flexibility and scalability of the program. For example, if we do not need to delegate, a function can only execute addition operations, however, if a delegate is used, it can execute addition, subtraction, multiplication, division at the same time. That is to say, the function of this program is extended, and the flexibility of work is also improved.

The following code is used to calculate the power and multiplication simultaneously:

Namespace ConsoleApplication16 {// create a delegate double DoubleOp (double x); class Program {static void Main (string [] args) {// instantiate two calculation methods: DoubleOp [] operations = {new DoubleOp (MathOperations. multiByTwo), new DoubleOp (MathOperations. square)}; for (int I = 0; I
 
  
The result after this code is run is as follows:
  

I read the source code once. If I don't quite understand it, I just knocked it out and let it run. Seeing the running result, I felt that the code was clear, mainly because I didn't clarify its logic, sometimes you can't just do it. If you do it first, you may understand it.

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.