【问题标题】:C# : Distinctions between various <string, string> CollectionsC#:各种 <string, string> 集合之间的区别
【发布时间】:2011-10-14 15:25:46
【问题描述】:

这是一个我也经常回来的问题:

对于某些 xyz 情况(通常绑定到 DropDownList),最好使用的 &lt;string, string&gt; 集合是什么?

有人有这方面的备忘单吗?各种选项之间区别的完整列表?

【问题讨论】:

  • 太棒了,我现在打印出来!
  • KeyValuePair 数组是 IEnumerable。
  • 有什么理由遗漏了 SortedList
  • 我很难找到使用 ListDictionary 的充分理由,它的功能很短,限制很长。
  • 另外,所有 .NET 数组实现了IListIList&lt;T&gt;,因此,通过扩展,ICollection&lt;T&gt;ICollectionIEnumerable&lt;T&gt; 和 @987654328 @。使用数组而不是另一个IList&lt;T&gt; 实现(如List&lt;T&gt;)的最大缺点是数组是固定大小的(它们上的IList.Add() 方法不起作用)。

标签: c# string generics collections dictionary


【解决方案1】:

如果您要绑定到下拉列表,您可能会考虑排序顺序,并且集合的大小(最有可能并且在大多数用例中)足够小以避免性能问题。在这些情况下,List&lt;KeyValuePair&lt;string, string&gt;&gt; 是一个非常简单的选择,尽管 BindingList 可能更适合绑定,尤其是在 WPF 中。

Tuple&lt;string, string&gt; 甚至可以替换KeyValuePair

此外,非泛型(非强类型)集合通常会在装箱时提供一些最差的性能(除了难以处理之外),如果您担心列表开销,您可以指定最大大小在创作上将​​其最小化。泛型类的另一个优点是它们实现了IEnumerable 以与 Linq 一起使用,并且根据我的经验,它们往往被更广泛地使用并且为您的同事所熟知。一般来说,应该有一种明显的方式来用一种语言做某事,而 .Net 社区选择了Dictionary&lt;string, string&gt; 而不是StringDictionary

您还可以添加扩展方法,使基本列表更方便:

public static class ListKeyValuePairExtensions
{
    public static void Add<S, T>(this List<KeyValuePair<S, T>> list, S key, T value)
    {
        list.Add(new KeyValuePair<S, T>(key, value));
    }
}

编辑:正如 Porges 所指出的,在此问题解决的情况下,非泛型结构的性能影响不是来自装箱和拆箱,但是仍然存在性能影响,请参阅this article 以获得快速基准。

【讨论】:

  • 如果您使用的是字符串,或者特别是特定于字符串的类,则不会出现装箱。
  • 嗯,这就是我添加“(非强类型)”的原因。显然 StringDictionary 是强类型但不是通用的。不过,如果有人读到这篇文章并感到困惑,请指出这一点很有用。
  • 仍然没有拳击比赛。引用类型从不(也不能)装箱。
  • 感谢您的反馈。我不时为我的 DropDownLists 使用过List&lt;KeyValuePair&lt;string, string&gt;&gt;,因为它保留了我插入的顺序。但我觉得这不是一个容易的选择...我想要一个集合,它给我list.Add("key", "value") 的简单性并保持与插入相同的顺序...至于Tuple&lt;string, string&gt;,我会调查它!
  • 您可以为 'List' 编写扩展方法,请参阅我编辑的答案。
【解决方案2】:

以下是我对此的了解程度:



    StringDictionary strDict = new StringDictionary();
        // + Strong Type
        // - lowercases the key
        // - random order ?
        // - older approach, .Net 1 which predates generics

    Dictionary<string, string> dict = new Dictionary<string, string>();
        // + Strong Type
        // - random order ?

    List<KeyValuePair<string, string>> listKVP = new List<KeyValuePair<string, string>>();
        // + Strong Type
        // + Keeps order as inserted
        // - more complex to instanciate and use

    Hashtable hash = new Hashtable();
        // Automatically sorted by hash code
        // Better for big collections
        // - not strong typed

    ListDictionary listDict = new ListDictionary();
        // + faster than Hashtable for small collections (smaller than 10)
        // - not strong typed

    HybridDictionary hybridDict = new HybridDictionary();
        // Better compromise if unsure of length of collection
        // - not strong typed

    OrderedDictionary orderDict = new OrderedDictionary();
        // + Keeps order as inserted
        // - not strong typed

    SortedDictionary<string, string> sortedDict = new SortedDictionary<string, string>();
        // + Strong Type
        // Automatically sorted by key
        // + faster lookup than the Dictionary [@987654321@]

    SortedList<string, string> sortedList = new SortedList<string, string>();
        // + Strong Type
        // Automatically sorted by key
        // Almost same as SortedDict, but can access by index []

    KeyValuePair<string, string>[] arrayKVP = new KeyValuePair<string, string>[123];
        // + Strong Type
        // + Keeps order as inserted
        // - Fixed size

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 2017-08-24
    相关资源
    最近更新 更多