Lambda expressions (lambda expression) ⭐⭐⭐⭐⭐

     Author

 lambda expression (lambda expression) is actually an anonymous function a representation form,

          That is not the function name of the function: parameter list => expression or statement block , in my opinion the main purpose is to simplify writing code, improve code readability and syntax sugar provided.

  Lambda expressions used in C # "=>" operator indicates read as "goes to",

          When using multi-parameter multi-statement "(x, y) => {} statement block;" block of statements and parameters are required from small brackets and brackets,

          Single statement may be the single parameter "X => statement;" without brackets; as the parameter for () => expression.   

the delegate  void delegateLambda ();
 the delegate  void delegateLambda2 ( int X);
 the delegate  void delegateLambda3 ( int X, int Y);
 Private  void btnCalculate_Click ( Object SENDER, EventArgs E) 
{ 
    delegateLambda Lam = () => MessageBox.Show ( " no parameter lambda expressions " ); 
    Lam (); 
    delegateLambda2 lam2 = X => 
    { 
        MessageBox.Show ( " single lambda expression parameter = X: " + x.ToString ()); 
    }; 
    lam2 (50);
    delegateLambda3 lam3 = (x, y) =>
    {
        string result = (x + y).ToString();
        MessageBox.Show(result);
    };
    lam3(10, 20);
}

It can also be realized commissioned by the lambda expression:

Whether traditional or anonymous methods Lambda expressions realization of the commission,

Principles are in the process of compiling, you create an object instance of a static method delegate.

That anonymous methods and Lambda expressions in the CIL are based on unnamed instances of delegation.

public partial class MainDisplay : Form
    {
        public MainDisplay()
        {
            InitializeComponent();
        }

        List<Student> studentList = new List<Student>();
        private void MainDisplay_Load(object sender, EventArgs e)
        {
            Student s1 = new Student() { Name = "张三", Id = "1", age = 13, ClassId = "5", Sex = "" };
            Student s2 = new Student() { Name = "李四", Id = "2", age = 14, ClassId = "5", Sex = "" };
            Student s3 = new Student() { Name = "王五", Id = "3", age = 17, ClassId = "2", Sex = "" };
            Student s4 = new Student() { Name = "赵六", Id = "4", age = 16, ClassId = "2", Sex = "" };
            studentList.Add(s1);
            studentList.Add(s2);
            studentList.Add(s3);
            studentList.Add(s4);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            List<Student> student=new List<Student>();
            //查询年龄大于30的学生
            student = studentList.Where(Stu => Stu.age > 15).ToList();
            foreach (Student stu in student)
            {
                MessageBox.Show(stu.Name);
            }
        }
    }

    public class Student
    {
        public string Id { get; set; }

        public string Name { get; set; }

        public string Sex { get; set; }

        public int age { get; set; }

        public string ClassId { get; set; }
    }

Guess you like

Origin www.cnblogs.com/ZkbFighting/p/12119693.html