37 #region 멀티캐스트 대리자 사용 샘플
38
39 delegate void Del(string s);
40
41 class TestClass {
42 static void Hello(string s) {
43 Console.WriteLine(" Hello, {0}!", s);
44 }
45
46 static void Goodbye(string s) {
47 Console.WriteLine(" Goodbye, {0}!", s);
48 }
49
50 public static void Test() {
51 Del a, b, c, d;
52
53 // Create the delegate object a that references
54 // the method Hello:
55 a = Hello;
56
57 // Create the delegate object b that references
58 // the method Goodbye:
59 b = Goodbye;
60
61 // The two delegates, a and b, are composed to form c:
62 c = a + b;
63
64 // Remove a from the composed delegate, leaving d,
65 // which calls only the method Goodbye:
66 d = c - a;
67
68 Console.WriteLine("Invoking delegate a:");
69 a("A");
70
71 Console.WriteLine("Invoking delegate b:");
72 b("B");
73
74 // A, B 둘다 호출됨
75 Console.WriteLine("Invoking delegate c:");
76 c("C");
77
78 // A, B 에서 A 가 빠진 B 만 호출됨
79 Console.WriteLine("Invoking delegate d:");
80 d("D");
81 }
82 }
83 /* Output:
84 Invoking delegate a:
85 Hello, A!
86 Invoking delegate b:
87 Goodbye, B!
88 Invoking delegate c:
89 Hello, C!
90 Goodbye, C!
91 Invoking delegate d:
92 Goodbye, D!
93 */
94
95 #endregion