Delegate là gì?
Khai báo delegate
1
| chỉ_định_từ_truy_xuất delegate kiểu_trả_về tên_delegate(danh_sách_tham_số); |
- chỉ_định_từ_truy_xuất: là một trong các thuộc tính truy cập: private, public, protected, internal.
- kiểu_trả_về: kiểu trả về của phương thức
- tên_delegate: tên của delegate
- danh_sách_tham_số: các tham số của phương thức
1
| public delegate void MyEventHandler( string Message); |
Để sử dụng delegate, chúng ta phải tạo một thể hiện của nó (tương tự như tạo đối tượng của lớp) và truyền vào phương thức phù hợp (kiểu trả về và tham số) vào hàm khởi tạo của nó. Ví dụ:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| using System; namespace DelegateDemo { public delegate void MyEventHandler( string msg); public class Demo { public static void Main() { Demo d = new Demo(); MyEventHandler handler = new MyEventHandler(d.DisplayMsg); handler( "Display Message Here." ); Console.ReadLine(); } public void DisplayMsg( string msg) { Console.WriteLine(msg); } } } |
Kỹ thuật Multicasting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| using System; namespace DelegateDemo { public delegate void MyEventHandler( string msg); public class Demo { public static void Main() { Demo d = new Demo(); MyEventHandler handler1 = new MyEventHandler(d.DisplayMsg); MyEventHandler handler2 = new MyEventHandler(d.DisplayMsg); MyEventHandler handler = handler1 + handler2; handler( "Test" ); Console.ReadLine(); } public void DisplayMsg( string msg) { Console.WriteLine(msg); } public void ShowHello( string name) { Console.WriteLine( "Hello " + name); } } } |
Ngoài toán tử “+”, C# còn hổ trợ toán tử “+=” và “-=” trên delegate. Ví dụ:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| using System; namespace DelegateDemo { public delegate void MyEventHandler( string msg); public class Demo { public static void Main() { Demo d = new Demo(); MyEventHandler handler = new MyEventHandler(d.DisplayMsg); handler += d.ShowHello; handler( "Test" ); Console.ReadLine(); } public void DisplayMsg( string msg) { Console.WriteLine(msg); } public void ShowHello( string name) { Console.WriteLine( "Hello " + name); } } } |
Event (sự kiện)
Cú pháp khai báo event trong C# như sau:
- chỉ_định_từ_truy_xuất: là một trong các thuộc tính truy cập: private, public, protected, internal.
- tên_delegate: kiểu của delegate đại diện cho event
- tên_event: tên của event
1
2
| public delegate void MyEventHandler( string msg); public event MyEventHandler Click; |
Ví dụ:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
| namespace DelegateDemo { public delegate void MyEventHandler( string msg); public class Window { public event MyEventHandler Click; public Window( int top, int left) { this .top = top; this .left = left; } // mô phỏng vẽ cửa sổ public virtual void DrawWindow() { } public void FireEvent() { if (Click != null ) Click( "Event Fire." ); } // Có hai biến thành viên private do đó hai // biến này sẽ không thấy bên trong lớp con int top; private int left; } // ListBox dẫn xuất từ Window public class ListBox : Window { public ListBox( int top, int left) : base (top, left) { //Console.WriteLine("Constructor's ListBox have 2 parameter"); } // Khởi dựng có tham số public ListBox( int top, int left, string theContents) : base (top, left) // gọi khởi dựng của lớp cơ sở { mListBoxContents = theContents; } public override void DrawWindow() { Console.WriteLine( "DrawWindow's ListBox" ); } // biến thành viên private private string mListBoxContents; } public class Tester { public static void Main() { Window w = new Window(100, 100); w.Click += w_Click; w.FireEvent(); // phát sinh sự kiện Console.ReadLine(); } static void w_Click( string msg) { Console.WriteLine(msg); } } } |
0 nhận xét:
Đăng nhận xét
Click to see the code!
To insert emoticon you must added at least one space before the code.