// ~ delegate void Kieu(object? sender, EventArgs args) public void TinhBinhPhuong(object sender, EventArgs e) { DuLieuNhap duLieuNhap = (DuLieuNhap)e; int i = duLieuNhap.Data; WriteLine($"Binh phuong cua {i} la {i * i}"); }
/* * Publisher -> class -> Phát sự kiện * Subscriber -> class -> Nhận sự kiện */ static void Main(string[] args) { CancelKeyPress += (sender, e) => { WriteLine(); WriteLine("Thoat chuong trinh"); }; // Publisher UserInput userInput = new UserInput(); userInput.suKienNhapSo += (sender, e) => { DuLieuNhap duLieuNhap = (DuLieuNhap)e; WriteLine("Ban vua nhap so: " + duLieuNhap.Data); }; // Subscriber TinhCan tinhCan = new TinhCan(); tinhCan.Sub(userInput); BinhPhuong binhPhuong = new BinhPhuong(); binhPhuong.Sub(userInput); userInput.Input(); }
// ~ delegate void Kieu(object? sender, EventArgs args) public void Can(object sender, EventArgs e) { DuLieuNhap duLieuNhap = (DuLieuNhap)e; int i = duLieuNhap.Data; WriteLine($"Can bac hai cua {i} la {Math.Sqrt(i)}"); }