Action, Func, Predicate in csharp

We know that delegation is very powerful. First of all, we have to use delegation to solve the "reverse transmission problem" of information between objects.

Secondly, there are actually many uses for delegation. (Anonymous method, Lambda expression, and generic combination) Finally, when we learn to a certain degree,

We must study the underlying principles. (For example, we learn EntityFramework, linq various queries, extension methods)

One, anonymous method, Lambda expression

1. The concept of anonymous methods: A method has no specific name, but only the keyword delegate, method parameters, and method body. This method is an anonymous method.

The advantage of anonymous methods: directly associate the specific method with the delegate. If we only need one method based on the delegate, the anonymous method must be simple.

2. Lambda expression: appeared in C#3.0. Use this expression to write code blocks more concisely.

      CalculatorDelegate cal3 = (int a, int b) => { return a + b; };
      CalculatorDelegate cal4 = (a, b) => a - b;

[1] In the Lambda expression, the parameter type can be an explicit type or an inferred type.
[2] If it is an inferred type, the parameter type can be automatically inferred by the compiler according to the context.
[3] Operator => read as goes to, the left side of the operator input parameters (if any), the right side is an expression or statement block.
[4] Two ways of expression:

         (Input args)=>expression
         (input args)=>{ statement 1; statement 2; statement 3.

Guess you like

Origin blog.csdn.net/qq_41617901/article/details/110749918