3 namespace MyCollections
5 using System
.Collections
;
7 // A delegate type for hooking up change notifications.
8 public delegate void ChangedEventHandler(object sender
, EventArgs e
);
10 // A class that works just like ArrayList, but sends event
11 // notifications whenever the list changes.
12 public class ListWithChangedEvent
: ArrayList
14 // An event that clients can use to be notified whenever the
15 // elements of the list change.
16 public event ChangedEventHandler Changed
;
18 // Invoke the Changed event; called whenever list changes
19 protected virtual void OnChanged(EventArgs e
)
25 // Override some of the methods that can change the list;
26 // invoke event after each
27 public override int Add(object value)
29 int i
= base.Add(value);
30 OnChanged(EventArgs
.Empty
);
34 public override void Clear()
37 OnChanged(EventArgs
.Empty
);
40 public override object this[int index
]
45 OnChanged(EventArgs
.Empty
);
57 private ListWithChangedEvent List
;
59 public EventListener(ListWithChangedEvent list
)
62 // Add "ListChanged" to the Changed event on "List".
63 List
.Changed
+= new ChangedEventHandler(ListChanged
);
66 // This will be called whenever the list changes.
67 private void ListChanged(object sender
, EventArgs e
)
69 Console
.WriteLine("This is called when the event fires.");
74 // Detach the event and delete the list
75 List
.Changed
-= new ChangedEventHandler(ListChanged
);
82 // Test the ListWithChangedEvent class.
83 public static void Main()
86 ListWithChangedEvent list
= new ListWithChangedEvent();
88 // Create a class that listens to the list's change event.
89 EventListener listener
= new EventListener(list
);
91 // Add and remove items from the list.
101 namespace MyCollections
103 using System
.Collections
;
105 // A class that works just like ArrayList, but sends event
106 // notifications whenever the list changes:
107 public class ListWithChangedEvent
: ArrayList
109 // An event that clients can use to be notified whenever the
110 // elements of the list change:
111 public event EventHandler Changed
;
113 // Invoke the Changed event; called whenever list changes:
114 protected virtual void OnChanged(EventArgs e
)
120 // Override some of the methods that can change the list;
121 // invoke event after each:
122 public override int Add(object value)
124 int i
= base.Add(value);
125 OnChanged(EventArgs
.Empty
);
129 public override void Clear()
132 OnChanged(EventArgs
.Empty
);
135 public override object this[int index
]
140 OnChanged(EventArgs
.Empty
);
152 private ListWithChangedEvent List
;
154 public EventListener(ListWithChangedEvent list
)
157 // Add "ListChanged" to the Changed event on "List":
158 List
.Changed
+= new EventHandler(ListChanged
);
161 // This will be called whenever the list changes:
162 private void ListChanged(object sender
, EventArgs e
)
164 Console
.WriteLine("This is called when the event fires.");
169 // Detach the event and delete the list:
170 List
.Changed
-= new EventHandler(ListChanged
);
177 // Test the ListWithChangedEvent class:
178 public static void Main()
180 // Create a new list:
181 ListWithChangedEvent list
= new ListWithChangedEvent();
183 // Create a class that listens to the list's change event:
184 EventListener listener
= new EventListener(list
);
186 // Add and remove items from the list: