为什么。net中的集合类不是通用的?

时间:2022-05-05 20:59:04

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:

为什么。net集合类仍然是非泛型的呢?例如,控制。ControlCollection并不实现IList ,只实现IList,或者FormCollection只实现upto ICollection,而不实现icection 。每次我需要在IEnumerable 上执行一些很酷的操作,或者利用Linq的细节,我都必须将集合类转换为它的等价的泛型类型,如下所示:

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..

考虑到这些集合在。net 1.1天之前就已经存在了,那么是否要保持向后兼容性呢?即使是这样,为什么这些集合(还有许多更多的)不能进一步实现泛型接口和现有的接口,我认为它们不会破坏向后兼容性。我不确定我是否遗漏了一些泛型的关键字,可能是我对这个想法不够透彻。

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

编辑:虽然我在只考虑WinForms时问过这个问题,但我发现它也适用于WPF等更新的技术。正如@Dennis指出,WPF也有非泛型集合。例如WindowCollection或ItemCollection。WPF是通过。net 3发布的,但是在。net 2:-o中引入了泛型

1 个解决方案

#1


8  

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

考虑到这些集合在。net 1.1天之前就已经存在了,那么是否要保持向后兼容性呢?

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

是的,但主要是因为WinForms从2005年甚至更早的时候就已经“功能齐全”了。

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.

WindowsCollection已经“专门化”,它包含从Window派生的类。这里继承是正确的模型,不需要泛型。

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.

而ItemCollection也有意是非通用的。这是一个精心设计的特性,它可以同时包含所有类型,甚至不同类型。再说一遍,泛型不是必需的,也不可取。

#1


8  

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

考虑到这些集合在。net 1.1天之前就已经存在了,那么是否要保持向后兼容性呢?

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

是的,但主要是因为WinForms从2005年甚至更早的时候就已经“功能齐全”了。

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.

WindowsCollection已经“专门化”,它包含从Window派生的类。这里继承是正确的模型,不需要泛型。

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.

而ItemCollection也有意是非通用的。这是一个精心设计的特性,它可以同时包含所有类型,甚至不同类型。再说一遍,泛型不是必需的,也不可取。