First off, ObservableCollection: What's the Added Value?
I want to do data binding. I can use ObservableCollection - but I don't have to. Data binding will work against anything IEnumerable. The added value of an ObservableCollection is that it implements INotifyCollectionChanged which raises an event whenever the collection is modified (i.e. an element is added, removed or replaced). There is no contract in place though for elements being modified as this is not an addition, removal or replacement. That's a gotcha.
Where is ObservableCollection?
If you've been off dot net a while you may forget. Is it in System.Collections, is it in System.ComponentModel? Neither. The namespace you need is System.Collections.ObjectModel. Not exactly intuitive, but logical when you think about it.
Philosophy of System.Collections.ObjectModel
The System.Collections.ObjModel namespace is famous for ObservableCollection (part of .Net 3 onwards and also Silverlight, recall WPF was born in .Net 3).
More generally, though, this namespace formalises the base classes for building collections (i.e. the implicit architecture underlying the Collection classes).
List based approach to Collections
For example. The base class for a GenericCollection is Collection<T>.
What properties do you think we might ascribe to a class to represent GenericCollections? What do we do with Collections?
We count the number of items (property Count), we want the items (property Items), anything else? You get an item at a specific index. Note, this a very list-oriented approach to building collections, rather than a set-oriented approach...in fact, GenericCollection inherits from IList<T>. Also, a generic collection can be initialise with an IList<T>.
Evolution 2.0: The ObservableCollection
ObservableCollections are a whole different ball game, providing notifications when objects are added and removed, and when the whole list is refreshed. The events an ObservableCollection (henceforth OC) exposes are:
* CollectionChanged (for the aforementioned change events), and
* PropertyChanged.
(This is actually enforced by the INotifyProperyChanged and INotifyCollectionChanged interfaces). ObservableCollection builds on GenericCollection by inheriting directly from Collection<T>. Its one of the building blocks of data binding.
I want to do data binding. I can use ObservableCollection - but I don't have to. Data binding will work against anything IEnumerable. The added value of an ObservableCollection is that it implements INotifyCollectionChanged which raises an event whenever the collection is modified (i.e. an element is added, removed or replaced). There is no contract in place though for elements being modified as this is not an addition, removal or replacement. That's a gotcha.
Where is ObservableCollection?
If you've been off dot net a while you may forget. Is it in System.Collections, is it in System.ComponentModel? Neither. The namespace you need is System.Collections.ObjectModel. Not exactly intuitive, but logical when you think about it.
Philosophy of System.Collections.ObjectModel
The System.Collections.ObjModel namespace is famous for ObservableCollection (part of .Net 3 onwards and also Silverlight, recall WPF was born in .Net 3).
More generally, though, this namespace formalises the base classes for building collections (i.e. the implicit architecture underlying the Collection classes).
List based approach to Collections
For example. The base class for a GenericCollection is Collection<T>.
What properties do you think we might ascribe to a class to represent GenericCollections? What do we do with Collections?
We count the number of items (property Count), we want the items (property Items), anything else? You get an item at a specific index. Note, this a very list-oriented approach to building collections, rather than a set-oriented approach...in fact, GenericCollection inherits from IList<T>. Also, a generic collection can be initialise with an IList<T>.
Evolution 2.0: The ObservableCollection
ObservableCollections are a whole different ball game, providing notifications when objects are added and removed, and when the whole list is refreshed. The events an ObservableCollection (henceforth OC) exposes are:
* CollectionChanged (for the aforementioned change events), and
* PropertyChanged.
(This is actually enforced by the INotifyProperyChanged and INotifyCollectionChanged interfaces). ObservableCollection builds on GenericCollection by inheriting directly from Collection<T>. Its one of the building blocks of data binding.