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
There is a ticket for this now: https://github.com/dotnet/winforms/issues/2644 (.NET Core of course).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With