Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are collections classes in .NET not generic?

After all the fuss about non-generic classes being obsolete (well almost) why are .NET collection classes still non-generic? For instance, Control.ControlCollection doesn't implement IList<T> but only IList, or FormCollection implements only upto ICollection and not ICollection<T>. Everytime I have to do some cool operation available on IEnumerable<T> or avail Linq niceties, I have to invariably convert the collection classes to its equivalent generic type like this:

this.Controls.OfType<Control>();

Its weird to have to specify Control in an operation like this on ControlCollection when the only thing it can hold is again a Control.

Is it to maintain backward compatibility, considering these collections existed back in .Net 1.1 days? Even if it is, why cant these collections (there are many many more) further implement generic interfaces along with the existing ones which I believe wouldnt break backward compatibility. I am unsure if I am missing something key to generics, may be I am not thorough with the idea..

Edit: Though I asked this when I had only WinForms in mind, I find this applies to newer technologies like WPF too. As @Dennis points out WPF too has non-generic collections. For instance WindowCollection or the ItemCollection. WPF was released along .NET 3, but generics was introduced in .NET 2 :-o

Update to this:

There is a ticket for this now: https://github.com/dotnet/winforms/issues/2644 (.NET Core of course).

like image 284
nawfal Avatar asked Sep 05 '12 12:09

nawfal


1 Answers

Is it to maintain backward compatibility, considering these collections existed back in .Net 1.1 days?

Yes, but mostly because WinForms has been 'Feature Complete' since 2005 or even earlier.

Feature Complete means it's on life-support, just short of do-not-resuscitate.

Additonal, re the Edit:

The WindowsCollection is already 'specialized', it contains classes derived from Window. Inheritance is the right model here, generics are not called for.

And the ItemCollection is intentionally non-generic as well. It is a deliberate feature that it can contain all types, even different types at the same time. Again, generics is not necessary and not desirable.

like image 110
Henk Holterman Avatar answered Nov 15 '22 15:11

Henk Holterman