泛型和System.Collections中 [英] Generics and System.Collections

查看:83
本文介绍了泛型和System.Collections中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

迁移到.NET 2.0 +后是有过一个理由仍然使用systems.Collections命名空间(除了保持传统的code)?如果仿制药空间总是被用来代替?

After moving to .NET 2.0+ is there ever a reason to still use the systems.Collections namespace (besides maintaining legacy code)? Should the generics namespace always be used instead?

推荐答案

在大多数情况下,一般的收藏品将执行比非泛型对口更快,让你有一个强类型集合的利益。在比较System.Collections中和System.Collections.Generic现有的收藏,你会得到下面的移民:

For the most part, the generic collections will perform faster than the non-generic counterpart and give you the benefit of having a strongly-typed collection. Comparing the collections available in System.Collections and System.Collections.Generic, you get the following "migration":


    Non-Generic               Generic Equivalent
    ------------------------------------------------------------
    ArrayList                 List<T>
    BitArray                  N/A
    CaseInsensitiveComparer   N/A
    CollectionBase            Collection<T>
    Comparer                  Comparer<T>
    DictionaryBase            Dictionary<TKey,TValue>
    Hashtable                 Dictionary<TKey,TValue>
    Queue                     Queue<T>
    ReadOnlyCollectionBase    ReadOnlyCollection<T>
    SortedList                SortedList<TKey,TValue>
    Stack                     Stack<T>

    DictionaryEntry           KeyValuePair<TKey,TValue>

    ICollection               N/A (use IEnumerable<T> or anything that extends it)
    IComparer                 IComparer<T>
    IDictionary               IDictionary<TKey,TValue>
    IEnumerable               IEnumerable<T>
    IEnumerator               IEnumerator<T>
    IEqualityComparer         IEqualityComparer<T>
    IList                     IList<T>

ICollection的是不变的(没有成员更改集合的内容),而ICollection的&LT; T&GT;是可变的。这使得在名称相似的接口仅在ICollection的和IEnumerable&LT; T&GT;由非常小的差异。

ICollection is immutable (no members to change the contents of the collection) while ICollection<T> is mutable. This makes the interfaces similar in name only while ICollection and IEnumerable<T> differ by very little.

从这份名单中,没有一个通用的对应唯一的非通用类是BitArray和CaseInsensitiveComparer。

From this list, the only non-generic classes that don't have a generic counterpart are BitArray and CaseInsensitiveComparer.

这篇关于泛型和System.Collections中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆