当你不使用泛型集合? [英] When would you not use Generic Collections?
问题描述
使用泛型的优点是,它增加了类型安全 - 你只能放置在正确类型的东西,你得到了正确的类型,而无需转换。我能想到的不使用泛型集合的唯一原因是,你需要存储一些任意的数据。我失去了一些东西?有什么其他的原因在那里,当使用集合处理不使用泛型?
The advantage of using generics is that it increases the type safety - you can only put in the correct type of thing, and you get out the correct type without requiring a cast. The only reason I can think of for not using generic collections is that you need to store some arbitrary data. Am I missing something? What other reasons are there to not use generics when dealing with collections?
推荐答案
如果你需要存储的任意数据,使用名单,其中,对象>
(或其他)。然后,这是绝对清楚的,它的的刻意的任意性。
If you need to store arbitrary data, use List<object>
(or whatever). Then it's absolutely clear that it's deliberately arbitrary.
除此之外,我不会使用非泛型集合的任何东西。我的有无的使用的IEnumerable
和的IList
时,我已经转换了对象
参考,不知道类型将其转换为在编译时 - 所以非一般的接口的是有用的,有时...但不非通用类本身。
Other than that, I wouldn't use the non-generic collections for anything. I have used IEnumerable
and IList
when I've been converting an object
reference and didn't know the type to cast it to at compile-time - so non-generic interfaces are useful sometimes... but not the non-generic classes themselves.
这篇关于当你不使用泛型集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!