LINQ

Cách sử dụng Extension Methods in c#

Extension Methods: là một kiểu các phương thức tĩnh đặc biệt.

Extension Methods : cho thêm 1 phương thức đã tồn tại. có nghĩa 1 lớp người ta đã viết trước rồi. Chúng ta có thể thêm vào lớp đó, không cần dùng lớp kế thừa và cũng không cần biên dịch code cũ.

Đối số đâu tiên dùng từ kháo this(int )> bất kỳ kiểu dữ liệu gì cũng có phương thức này

         vd: int à 1 lớp kiểu dữ liệu. cú phát static abc có kiểu dữ liệu int. trước kiểu dữ liệu ta để từ khóa this

Nó tự động cài kiểu int vào hàm abc. không có đói số truyền vào this.

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;

using ClassLibrary1;

namespace Extension_Methods

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }       

        private void Form1_Load(object sender, EventArgs e)

        {

        }

        private void btntong_Click(object sender, EventArgs e)

        {

            //int n = int.Parse(txtMaN.Text);

            int n = txtMaN.Laygiatriin();// lấy giá trị int của textbox , tất cả các text box đều lấy giá trị int

            lbthienthi.Text = n.TinhTongn() + “”;

        }

        private void button1_Click(object sender, EventArgs e)

        {

            lblKetqua.Text = txtc1.Text.noichuoi(txtc2.Text);

            // ta cài hàm nối chuỗi vào kiểu dữ liệu string

            btntong.tomado();

        }

        private void button2_Click(object sender, EventArgs e)

        {

            SinhVien teo = new SinhVien();

            teo.Ma = 1;

            teo.Ten = “Tèo”;

            teo.NamSinh = new DateTime(2016, 1, 1);

            int tuoi = teo.tinhtuoi();

            bttinhtuoi.Text = tuoi.ToString();

        }

    }

}


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace Hoclinqforeach

{

 public    class SanPham

    {

        public string Ma { get; set; }

        public string Ten { get; set; }

      
        public datetime Hạndung { get; set; }

    }

}

video hướng dẫn sử dụng Extension Methods