Put differently:
换句话说:
Is there a good reason to choose a loosely-typed collection over a type-safe one (HashTable vs. Dictionary)? Are they still there only for compatibility?
是否有充分的理由选择一个松散类型的集合而不是类型安全的集合(HashTable vs. Dictionary)?它们还仅仅是为了兼容性吗?
As far as I understand, generic collections not only are type-safe, but their performance is better.
据我所知,泛型集合不仅是类型安全的,而且它们的性能更好。
Here's a comprehensive article on the topic: An Extensive Examination of Data Structures Using C# 2.0.
这里有一篇关于这个主题的全面文章:使用c# 2.0对数据结构进行广泛的检查。
8 个解决方案
#1
21
The non-generic collections are so obsolete that they've been removed from the CoreCLR used in Silverlight and Live Mesh.
非通用集合是如此的过时以至于它们已经从Silverlight和Live Mesh中使用的CoreCLR中移除。
#2
16
There are also issues with COM visibility - COM interop can't be used with generics
还有一些关于COM可见性的问题——COM互操作不能用于泛型
#3
15
Going forward only generic collections should be used. There is also the benefit of avoiding boxing/unboxing of types in the collection. This is ineffecient, especially when you have a collection of value types that are converted to System.Object when stored on the collection, hence storing the values on the heap instead of the callstack.
接下来,应该只使用泛型集合。避免集合中类型的装箱/拆箱也有好处。这是无效的,尤其是当您拥有转换为System的值类型集合时。对象存储在集合上时,因此将值存储在堆上而不是调用堆栈上。
#4
7
With regard to using non-generic collections for storing heterogeneous collections of stuff, you can always use List<object> to accomplish the same thing. For this reason alone, I'd say there's almost no reason at all to touch the non-generic collections ever again.
对于使用非泛型集合来存储异构的集合,您总是可以使用List
The exception to this would be to maintain compatibility with systems written in other languages, or against previous versions of the .NET framework, but that's a pretty "edgy" case if you ask me.
例外的情况是,要保持与用其他语言编写的系统的兼容性,或者与. net框架的以前版本兼容,但如果你问我的话,这是一个相当“尖锐”的问题。
#5
2
I can tell you that XAML serialization of collections rely on them implementing either IList or IDictionary, so non-generic collections are going to be with us for some time to come.
我可以告诉您,XAML序列化的集合依赖于它们实现IList或IDictionary,因此非泛型集合将在未来一段时间内与我们共存。
#6
1
I wouldn't jump and say that are obsolete or are going to be removed anytime soon. It's true that you should avoid using non-generic collections unless you have a reason not not use a generic version. Thousands of lines of legacy (not so legacy) code is still floating around (and will be for years) that support non-generic collections such as ArrayLists. Since these were the only collections in .NET 1.0 and 1.1, it has been widely used (and abused) throughout the year.
我不会突然说它已经过时了,或者很快就会被移除。确实,您应该避免使用非泛型集合,除非您有理由不使用泛型版本。数千行遗留代码(而不是遗留代码)仍然在(并且将会持续数年)中浮动,支持非泛型集合,如arraylist。因为这是。net 1.0和1.1中唯一的集合,所以在一年中它被广泛使用(并被滥用)。
I still occasionally have to interact with an old O/R mapper written in .NET 1.1 that returns IList objects. I have a method that does the conversion to a generic List<>, which is not efficient, but that's the way it is.
我仍然偶尔需要与在. net 1.1中编写的旧的O/R映射器进行交互,以返回IList对象。我有一个方法,可以将其转换为泛型列表<>,这是无效的,但它就是这样。
And if you need to store different objects in the same array (weird but possible) you will need a non-generic collection. The penalty of Boxing and Unboxing is something you'll have to pay anyway.
如果需要在同一个数组中存储不同的对象(奇怪但可能),则需要一个非泛型集合。拳击和反拳击的惩罚是你无论如何都要付出的代价。
Don't be afraid to use them if you feel that you have to.
如果你觉得你必须这样做,不要害怕使用它们。
#7
1
There might be instances where you need to store objects of unknown types, or objects of multiple different types, but if you do indeed know the type of the objects that you want to store then I cannot see a reason not to use the generic version.
在某些情况下,您可能需要存储未知类型的对象,或多个不同类型的对象,但是如果您确实知道要存储的对象的类型,那么我看不出不使用通用版本的理由。
Edit: As commented you can just use List<Object>
- doh!
编辑:正如注释你可以使用列表
#8
0
Yes, as far as I understand they are only there for compatibility with existing products. You should always use the type safe version (i.e. use System.Collections.Generic over System.Collections).
是的,据我所知,它们只是为了与现有产品兼容。您应该始终使用类型安全版本(即使用System.Collections)。通用System.Collections)。
http://msdn.microsoft.com/en-us/library/ms379564.aspx
http://msdn.microsoft.com/en-us/library/ms379564.aspx
#1
21
The non-generic collections are so obsolete that they've been removed from the CoreCLR used in Silverlight and Live Mesh.
非通用集合是如此的过时以至于它们已经从Silverlight和Live Mesh中使用的CoreCLR中移除。
#2
16
There are also issues with COM visibility - COM interop can't be used with generics
还有一些关于COM可见性的问题——COM互操作不能用于泛型
#3
15
Going forward only generic collections should be used. There is also the benefit of avoiding boxing/unboxing of types in the collection. This is ineffecient, especially when you have a collection of value types that are converted to System.Object when stored on the collection, hence storing the values on the heap instead of the callstack.
接下来,应该只使用泛型集合。避免集合中类型的装箱/拆箱也有好处。这是无效的,尤其是当您拥有转换为System的值类型集合时。对象存储在集合上时,因此将值存储在堆上而不是调用堆栈上。
#4
7
With regard to using non-generic collections for storing heterogeneous collections of stuff, you can always use List<object> to accomplish the same thing. For this reason alone, I'd say there's almost no reason at all to touch the non-generic collections ever again.
对于使用非泛型集合来存储异构的集合,您总是可以使用List
The exception to this would be to maintain compatibility with systems written in other languages, or against previous versions of the .NET framework, but that's a pretty "edgy" case if you ask me.
例外的情况是,要保持与用其他语言编写的系统的兼容性,或者与. net框架的以前版本兼容,但如果你问我的话,这是一个相当“尖锐”的问题。
#5
2
I can tell you that XAML serialization of collections rely on them implementing either IList or IDictionary, so non-generic collections are going to be with us for some time to come.
我可以告诉您,XAML序列化的集合依赖于它们实现IList或IDictionary,因此非泛型集合将在未来一段时间内与我们共存。
#6
1
I wouldn't jump and say that are obsolete or are going to be removed anytime soon. It's true that you should avoid using non-generic collections unless you have a reason not not use a generic version. Thousands of lines of legacy (not so legacy) code is still floating around (and will be for years) that support non-generic collections such as ArrayLists. Since these were the only collections in .NET 1.0 and 1.1, it has been widely used (and abused) throughout the year.
我不会突然说它已经过时了,或者很快就会被移除。确实,您应该避免使用非泛型集合,除非您有理由不使用泛型版本。数千行遗留代码(而不是遗留代码)仍然在(并且将会持续数年)中浮动,支持非泛型集合,如arraylist。因为这是。net 1.0和1.1中唯一的集合,所以在一年中它被广泛使用(并被滥用)。
I still occasionally have to interact with an old O/R mapper written in .NET 1.1 that returns IList objects. I have a method that does the conversion to a generic List<>, which is not efficient, but that's the way it is.
我仍然偶尔需要与在. net 1.1中编写的旧的O/R映射器进行交互,以返回IList对象。我有一个方法,可以将其转换为泛型列表<>,这是无效的,但它就是这样。
And if you need to store different objects in the same array (weird but possible) you will need a non-generic collection. The penalty of Boxing and Unboxing is something you'll have to pay anyway.
如果需要在同一个数组中存储不同的对象(奇怪但可能),则需要一个非泛型集合。拳击和反拳击的惩罚是你无论如何都要付出的代价。
Don't be afraid to use them if you feel that you have to.
如果你觉得你必须这样做,不要害怕使用它们。
#7
1
There might be instances where you need to store objects of unknown types, or objects of multiple different types, but if you do indeed know the type of the objects that you want to store then I cannot see a reason not to use the generic version.
在某些情况下,您可能需要存储未知类型的对象,或多个不同类型的对象,但是如果您确实知道要存储的对象的类型,那么我看不出不使用通用版本的理由。
Edit: As commented you can just use List<Object>
- doh!
编辑:正如注释你可以使用列表
#8
0
Yes, as far as I understand they are only there for compatibility with existing products. You should always use the type safe version (i.e. use System.Collections.Generic over System.Collections).
是的,据我所知,它们只是为了与现有产品兼容。您应该始终使用类型安全版本(即使用System.Collections)。通用System.Collections)。
http://msdn.microsoft.com/en-us/library/ms379564.aspx
http://msdn.microsoft.com/en-us/library/ms379564.aspx