Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't INotifyCollectionChanged generic?

Tags:

c#

.net

wpf

If I create a new ObservableCollection<T>, and a CollectionChanged listener as follows:

var c = new ObservableCollection<MyType>();
c.CollectionChanged += new NotifyCollectionChangedEventHandler(h);
...
void h(object sender, NotifyCollectionChangedEventArgs e) 
{
    IList newItems = e.NewItems;
    // non generic IList!  :(
}

Why isn't e.NewItems an IList<MyType>?

like image 748
funkybro Avatar asked Aug 31 '25 18:08

funkybro


2 Answers

The ObservableCollection is designed to support databinding scenarios in platforms like WPF where the databound controls don't care about the type of the collection they're bound to. Making the notifications generic would only make it much harder to write the controls without giving any benefit.

like image 65
Gabe Avatar answered Sep 02 '25 07:09

Gabe


Presumably so that it can be used for non-generic collections as well as ObservableCollection<T>

like image 32
Steve Greatrex Avatar answered Sep 02 '25 09:09

Steve Greatrex