What is the best way to iterate over a dictionary?

前端 未结 30 1761
我寻月下人不归
我寻月下人不归 2020-11-22 05:18

I\'ve seen a few different ways to iterate over a dictionary in C#. Is there a standard way?

相关标签:
30条回答
  • 2020-11-22 05:50

    foreach is fastest and if you only iterate over ___.Values, it is also faster

    0 讨论(0)
  • 2020-11-22 05:51

    I know this is a very old question, but I created some extension methods that might be useful:

        public static void ForEach<T, U>(this Dictionary<T, U> d, Action<KeyValuePair<T, U>> a)
        {
            foreach (KeyValuePair<T, U> p in d) { a(p); }
        }
    
        public static void ForEach<T, U>(this Dictionary<T, U>.KeyCollection k, Action<T> a)
        {
            foreach (T t in k) { a(t); }
        }
    
        public static void ForEach<T, U>(this Dictionary<T, U>.ValueCollection v, Action<U> a)
        {
            foreach (U u in v) { a(u); }
        }
    

    This way I can write code like this:

    myDictionary.ForEach(pair => Console.Write($"key: {pair.Key}, value: {pair.Value}"));
    myDictionary.Keys.ForEach(key => Console.Write(key););
    myDictionary.Values.ForEach(value => Console.Write(value););
    
    0 讨论(0)
  • 2020-11-22 05:51

    Sometimes if you only needs the values to be enumerated, use the dictionary's value collection:

    foreach(var value in dictionary.Values)
    {
        // do something with entry.Value only
    }
    

    Reported by this post which states it is the fastest method: http://alexpinsker.blogspot.hk/2010/02/c-fastest-way-to-iterate-over.html

    0 讨论(0)
  • 2020-11-22 05:52

    Simplest form to iterate a dictionary:

    foreach(var item in myDictionary)
    { 
        Console.WriteLine(item.Key);
        Console.WriteLine(item.Value);
    }
    
    0 讨论(0)
  • 2020-11-22 05:52
    var dictionary = new Dictionary<string, int>
    {
        { "Key", 12 }
    };
    
    var aggregateObjectCollection = dictionary.Select(
        entry => new AggregateObject(entry.Key, entry.Value));
    
    0 讨论(0)
  • 2020-11-22 05:53

    Depends on whether you're after the keys or the values...

    From the MSDN Dictionary(TKey, TValue) Class description:

    // When you use foreach to enumerate dictionary elements,
    // the elements are retrieved as KeyValuePair objects.
    Console.WriteLine();
    foreach( KeyValuePair<string, string> kvp in openWith )
    {
        Console.WriteLine("Key = {0}, Value = {1}", 
            kvp.Key, kvp.Value);
    }
    
    // To get the values alone, use the Values property.
    Dictionary<string, string>.ValueCollection valueColl =
        openWith.Values;
    
    // The elements of the ValueCollection are strongly typed
    // with the type that was specified for dictionary values.
    Console.WriteLine();
    foreach( string s in valueColl )
    {
        Console.WriteLine("Value = {0}", s);
    }
    
    // To get the keys alone, use the Keys property.
    Dictionary<string, string>.KeyCollection keyColl =
        openWith.Keys;
    
    // The elements of the KeyCollection are strongly typed
    // with the type that was specified for dictionary keys.
    Console.WriteLine();
    foreach( string s in keyColl )
    {
        Console.WriteLine("Key = {0}", s);
    }
    
    0 讨论(0)
提交回复
热议问题