C × lambda expression

In versions of C before 2.0, the only way to declare a delegate was to use a named method. C ා 2.0 introduces anonymous methods, while in C ා 3.0 and later, Lambda expressions replace anonymous methods as the preferred way to write inline code. In one case, anonymous methods provide functionality that is not available in Lambda expressions. You can use anonymous methods to ignore parameter lists. This means that anonymous methods can be converted to delegates with various signatures. This is not possible for Lambda expressions. For more specific information about Lambda expressions, see Lambda expression (C ා Programming Guide).  

Microsoft tells you: we have delegation before C ා 2.0, anonymous method after 2.0, Lambda expression after C 񖓿 3.0. The order between them is: delegation - > anonymous expression - > Lambda expression

The Commission is as follows:

 delegate int calculator(int x, int y); //Delegate type
        static void Main()
        {
            calculator cal = new calculator(Adding);
            int He = cal(1, 1);
            Console.Write(He);
        }

        /// <summary>
        //Addition
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static int Adding(int x, int y)
        {
            return x + y;
        }

Anonymous methods are as follows:

 delegate int calculator(int x, int y); //Entrust
        static void Main()
        {
            calculator cal = delegate(int num1,int num2)
            {
                return num1 + num2;
            };
            int he = cal(1, 1);
            Console.Write(he);
        }

Let's talk about Lambda expressions:

According to the above addition, we use Lambda expression to implement the code as follows:

delegate int calculator(int x, int y); //Delegate type
        static void Main()
        {
            calculator cal = (x, y) => x + y;//Lambda expression, we found no, the code is more concise than one
            int he = cal(1, 1);
            Console.Write(he);
        }

Let's talk about Lambda expression in detail:

To create a lambda expression, you need to specify the input parameter (if any) on the left side of the lambda operator = > and then enter the expression or statement block on the other side. For example, the lambda expression x = > x * x specifies a parameter named X and returns the square value of X. As shown in the example above, you can assign this expression to a delegate type:

Lambda expression is a special anonymous function, which is an efficient expression similar to functional programming. Lambda simplifies the amount of code to be written in development. It can contain expressions and statements, and can be used to create delegate or expression tree types, supporting inline expressions with input parameters that can be bound to a delegate or expression tree. All lambda expressions use the lambda operator = > which reads "goes to.". The left side of the lambda operator is the input parameter, if any, and the right side is the expression or statement block. Lambda expression x = > x * x reads "x goes to x times x". Take a few simple lambda expressions as follows:

delegate bool MyBol(int x, int y);
        delegate bool MyBol_2(int x, string y);
        delegate int calculator(int x, int y); //Delegate type
        delegate void VS();
        static void Main()
        {
            MyBol Bol = (x, y) => x == y;
            MyBol_2 Bol_2 = (x, s) => s.Length > x;
            calculator C = (X, Y) => X * Y;
            VS S = () => Console.Write("I have no parameters Labada Expression");
            //
            int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
            int oddNumbers = numbers.Count(n => n % 2 == 1);
            //
            List<People> people = LoadData();//Initialization
            IEnumerable<People> results = people.Where(delegate(People p) { return p.age > 20; });
        }

        private static List<People> LoadData()
        {
            List<People> people = new List<People>();   //Create generic objects  
            People p1 = new People(21, "guojing");       //Create an object  
            People p2 = new People(21, "wujunmin");     //Create an object  
            People p3 = new People(20, "muqing");       //Create an object  
            People p4 = new People(23, "lupan");        //Create an object  
            people.Add(p1);                     //Add an object  
            people.Add(p2);                     //Add an object  
            people.Add(p3);                     //Add an object  
            people.Add(p4);
            return people;
        }

    }

    public class People
    {
        public int age { get; set; }                //set a property  
        public string name { get; set; }            //set a property  
        public People(int age, string name)      //Set properties (constructor construction)  
        {
            this.age = age;                 //Initialize property value age  
            this.name = name;               //Initialization property value name  
        }
    } 

Func < T > Commission

T is a parameter type, which is a delegate of generic type. It is very convenient to use.

First of all

  static void Main(string[] args)
        {
            Func<int, string> gwl = p => p + 10 + "--The return type is string";            
            Console.WriteLine(gwl(10) + "");   //Print '20 -- return type is string', z corresponds to parameter b, p corresponds to parameter a
            Console.ReadKey();
        }

Note: we can see that p here is an int type parameter, but the lambda body returns a string type parameter.

Another example

        static void Main(string[] args)
        {
            Func<int, int, bool> gwl = (p, j) =>
                {
                    if (p + j == 10)
                    {
                        return true;
                    }
                    return false;
                };
            Console.WriteLine(gwl(5,5) + "");   //Print "True", z corresponds to parameter b, p corresponds to parameter a
            Console.ReadKey();
        }

Note: from this example, we can see that p is of type int, j is of type int, and the return value is of type bool.

Published 35 original articles, won praise 18, visited 40000+
Private letter follow

Keywords: Lambda calculator Programming P4

Added by mars_rahul on Mon, 09 Mar 2020 11:20:43 +0200