Reputation: 87
I have recently bought a book called "The c# programming language" 4th edition. On page 49 there is a reference to a List having a changed event that can be bound to as an example of events. Have tried to reproduce this but ended up doing some head scratching with this :-
List<string> names = new List<string>();
names.Changed+= new EventHandler(ListChanged);
and so it continues.... Am I missing something or does List not have a changed event?
Upvotes: 4
Views: 543
Reputation: 86728
Indeed, it does. However, the List<T>
with a Changed
event referenced on page 49 is actually introduced on page 40. It is not the List<T>
that is part of the base class library. To use the version included in the book you will have to type it in yourself (or maybe the book comes with a CD, or perhaps you can download it), and your code will not be compatible any other code that uses the System.Collections.Generic.List<T>
that everybody else uses.
Upvotes: 1
Reputation: 17701
You can use ObservableCollection and you can add the namespace for using those
Namespace: System.Collections.ObjectModel
Assembly: WindowsBase (in WindowsBase.dll)
Upvotes: 0
Reputation: 51644
There is no such event on List<T>
. Maybe the book is refering to System.Collections.ObjectModel.ObservableCollection<T>
?
Upvotes: 0
Reputation: 1038940
No, List<T>
doesn't have such event, you are not missing anything. You may checkout the ObservableCollection<T>
which has a CollectionChanged
event you could subscribe to. It is extensively used in WPF and Silverlight to implement the MVVM pattern.
Upvotes: 6