LINQ

Hướng dẫn sử dụng Lamda expression trong c#

Lambda expression : Tạo ra những phương thức Anonymous Types, những phương thức nặc danh, Anonymous Types kết hơp với delegate tạo ra phương thưc nặc danh không có tên hàm, giống như anymous Types tạo ra lớp mà lớp không có tên.

Cú pháp lamda là => giúp tạo ra phương thức là anymous types :=> là nội suy kiểu dữ liệu.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace lamda

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

 

        }

        public delegate int kieuint(int x);

        public delegate int tongdelegate(int x, int y);

        public int tang2(int x)

        {

            return x + 2;

        }

        public int Giam2(int x)

        {

            return x – 2;

        }

        private void button1_Click(object sender, EventArgs e)

        {

            kieuint k1 = (int x) => x + 2          

kieuint k2 = x => x + 2;// tự động nội suy ra phương thức

            kieuint k3 = new kieuint(tang2);

            kieuint k4 = delegate (int x)

            {

                return x + 2;

            };

            tongdelegate k5 = (x, y) => x + y;

            tongdelegate k6 = (x, y) => x / y  ;             label1.Text = k5(3, 4) + “”;

        }

    }

}