Delegate trong C | 85 bài học lập trình C hay nhất

6 107 0
Delegate trong C  | 85 bài học lập trình C  hay nhất

Đang tải... (xem toàn văn)

Thông tin tài liệu

http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Delegate C# Delegate C# tương tự trỏ tới hàm, C C++ MộtDelegate biến kiểu tham chiếu mà giữ tham chiếu tới phương thức Tham chiếu thay đổi runtime Đặc biệt, delegate sử dụng để triển khai kiện phương thức call-back Tất delegate kế thừa cách ngầm định từ lớp System.Delegate C# Khai báo Delegate C# Khai báo Delegate C# định phương thức mà tham chiếu Delegate Một Delegate tham chiếu tới phương thức, mà có dấu hiệu Delegate Ví dụ, bạn xét delegate sau đây: public delegate int MyDelegate (string s); Delegate sử dụng để tham chiếu phương thức mà có tham sốstring đơn trả biến kiểu int Cú pháp để khai báo delegate C# là: delegate Khởi tạo Delegate C# Khi kiểu delegate khai báo, đối tượng delegate phải tạo với từ khóanew liên kết với phương thức cụ thể Khi tạo delegate, tham số truyền tới biểu thức new viết tương tự lời gọi phương thức, khơng có tham số tới phương thức Ví dụ: public delegate void printString(string s); printString ps1 = new printString(WriteToScreen); printString ps2 = new printString(WriteToFile); Ví dụ sau minh họa cách khai báo, thuyết minh sử dụng delegate mà sử dụng để tham chiếu phương thức mà nhận tham số integer trả giá trị integer http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com using System; delegate int NumberChanger(int n); namespace DelegateAppl { class TestDelegate { static int num = 10; public static int AddNum(int p) { num += p; return num; } public static int MultNum(int q) { num *= q; return num; } public static int getNum() { return num; } static void Main(string[] args) { //create delegate instances NumberChanger nc1 = new NumberChanger(AddNum); NumberChanger nc2 = new NumberChanger(MultNum); //calling the methods using the delegate objects nc1(25); Console.WriteLine("Value of Num: {0}", getNum()); http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com nc2(5); Console.WriteLine("Value of Num: {0}", getNum()); Console.ReadKey(); } } } Khi code biên dịch thực thi, cho kết quả: Value of Num: 35 Value of Num: 175 Multicast Delegate C# Các đối tượng Delegate hợp thành sử dụng toán tử “+” Một delegate hợp thành gọi hai Delegate mà hợp thành từ Chỉ có delegate kiểu hợp thành Tốn tử “-” sử dụng để gỡ bỏ delegate thành phần từ delegate hợp thành Sử dụng thuộc tính delegate, bạn tạo danh sách triệu hồi phương thức mà gọi delegate triệu hồi Điều gọi làMulticasting Delegate Chương trình ví dụ sau minh họa Multicasting Delegate C#: using System; delegate int NumberChanger(int n); namespace DelegateAppl { class TestDelegate { static int num = 10; public static int AddNum(int p) { num += p; return num; } http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com public static int MultNum(int q) { num *= q; return num; } public static int getNum() { return num; } static void Main(string[] args) { //create delegate instances NumberChanger nc; NumberChanger nc1 = new NumberChanger(AddNum); NumberChanger nc2 = new NumberChanger(MultNum); nc = nc1; nc += nc2; //calling multicast nc(5); Console.WriteLine("Value of Num: {0}", getNum()); Console.ReadKey(); } } } Khi code biên dịch thực thi, cho kết quả: Value of Num: 75 http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com Cách sử dụng Delegate C# Ví dụ sau minh họa cách sử dụng delegate C# Delegate với tên printString sử dụng để tham chiếu phương thức mà nhận chuỗi input khơng trả Chúng ta sử dụng delegate để gọi hai phương thức: phương thức in chuỗi tới Console, phương thức thứ hai in tới File using System; using System.IO; namespace DelegateAppl { class PrintString { static FileStream fs; static StreamWriter sw; // delegate declaration public delegate void printString(string s); // this method prints to the console public static void WriteToScreen(string str) { Console.WriteLine("The String is: {0}", str); } //this method prints to a file public static void WriteToFile(string s) { fs = new FileStream("c:\\message.txt", FileMode.Append, FileAccess.Write); sw = new StreamWriter(fs); sw.WriteLine(s); sw.Flush(); http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com sw.Close(); fs.Close(); } // this method takes the delegate as parameter and uses it to // call the methods as required public static void sendString(printString ps) { ps("Hello World"); } static void Main(string[] args) { printString ps1 = new printString(WriteToScreen); printString ps2 = new printString(WriteToFile); sendString(ps1); sendString(ps2); Console.ReadKey(); } } } Khi code biên dịch thực thi, cho kết quả: The String is: Hello World http://vietjack.com/ Trang chia sẻ học online miễn phí Page ... http://vietjack.com/ Trang chia sẻ h c online miễn phí Page http://vietjack.com/csharp/index.jsp Copyright © vietjack.com C ch sử dụng Delegate C# Ví dụ sau minh họa c ch sử dụng delegate C# Delegate với tên printString... static void Main(string[] args) { //create delegate instances NumberChanger nc; NumberChanger nc1 = new NumberChanger(AddNum); NumberChanger nc2 = new NumberChanger(MultNum); nc = nc1; nc += nc2;...http://vietjack.com/csharp/index.jsp Copyright © vietjack.com using System; delegate int NumberChanger(int n); namespace DelegateAppl { class TestDelegate { static int num = 10; public static int AddNum(int

Ngày đăng: 02/12/2017, 20:00

Mục lục

  • Delegate trong C#

    • Khai báo Delegate trong C#

    • Khởi tạo Delegate trong C#

    • Multicast một Delegate trong C#

    • Cách sử dụng Delegate trong C#

Tài liệu cùng người dùng

Tài liệu liên quan