Delegate, anonymous methods, Lambda expressions

1. anonymous methods

  No specific name, only the delegate keyword, method parameters, method body, so called anonymous methods

  The method allows anonymous code block (Method portion) as variable parameters are passed through a delegate, the method as defined in place of the independent

  to sum up:

  To delegate the beginning of the method body, followed by the parameter list, followed by curly braces to write the method body, and finally end with a semicolon

  Anonymous method is a method as defined delegate variable two steps together, the method is omitted individually defined

  If there is a method of a delegate type of parameter may be directly anonymous method as an argument.

1          // declare a delegate 
2          public  the delegate  int MyDelegate ( int A, int B);
 . 3  
. 4          static  void the Main ( String [] args)
 . 5          { // anonymous methods 
. 6              MyDelegate objDel = the delegate ( int A, int B)
 . 7              {
 . 8                  return A + B;
 . 9              };
 10              Console.WriteLine (objDel ( 10 , 20 is ));
11             Console.ReadLine();
12         }
Anonymous Methods

2.lambda expression

  C # 3.0 introduced lambda expressions, lambda expressions are anonymous methods used in a way more Introduction

  (Parameter list) => {} method body => goes to read as

  lambda expression parameter type parameter list may be "clear" type or "type inference" (you can omit the parameter type)

  If the type is inferred data type of the compiler automatically inferred by the context

  Under normal circumstances, or write parameter type of good, easy to understand themselves and others

  If only one parameter may be omitted (); if one-line method thereof may be omitted {}

 

3.lambda expression summary

  Two ways: (input args) => expression (input args) => {statement 1; statement 2; ......}

  Example 1: when only one parameter, which are optional Func <int, bool> del1 = (x) => {return x> 0;};

  Example 2: simplified as Func <int, bool> del2 = x => x> 0;

  Example 3: two or more input parameters are separated by commas Func <int, int, bool> del3 = (x, y) => x == y;

  Example 4: When no parameters, an empty parentheses direct input Action del4 = () => {...}

1          // declare a delegate 
2          public  the delegate  int MyDelegate ( int A, int B);
 . 3          static  void the Main ( String [] args)
 . 4          {
 . 5              // a statement is not omitted} { 
. 6              MyDelegate objDel0 = ( int A, int B) => { return A + B;};
 . 7              // statement omitted} { 
. 8              MyDelegate objDel1 = ( int A, int B) => A + B;
 . 9             Console.WriteLine(objDel0(10, 20));
10             Console.WriteLine(objDel1(10, 20));
11             Console.ReadLine();
12         }
Lambda expressions

4.lambda compared with an anonymous method

  Lambda expressions is itself an anonymous method

  Lambda expressions permit parameter can not specify the type parameter, the parameter must clearly indicate the anonymous method parameter types

  Lambda expressions method body allows the expression of a single or more statements, the method does not allow anonymous single expression form

 

Guess you like

Origin www.cnblogs.com/yangmengke2018/p/10939667.html