LogIn
I don't have account.

How to iterate a Dictionary in C# ?

DevSniper
156 Views

1. Using foreach Loop

2. Using For Loop

3. Using ParallelEnumerable.ForAll Method

4. String.Join Method

In C# , Dictionary is unordered Data structure,which means that you cann't expect that the return values of a Dictionary will be in same order every time. So when you loop/iterate it more time it can print values in different order and order of values unpredictable.

Using foreach Loop

I
using System.IO;
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Dictionary < string,string> dict = new Dictionary<string,string> {
            {"language","C#"},
            {"topic","Dictionary"},
            {"label","Beginner"}
        };
        foreach(KeyValuePair<string,string> kvp in dict) {
            Console.WriteLine( $"Key : {kvp.Key} Value : {kvp.Value}");
        }
    }
}
Key : language Value : C#
Key : topic Value : Dictionary
Key : label Value : Beginner
II
using System.IO;
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Dictionary < string,string> dict = new Dictionary<string,string> {
            {"language","C#"},
            {"topic","Dictionary"},
            {"label","Beginner"}
        };
        foreach(var item in dict) {
            Console.WriteLine($"item : {item} \n\t Key {item.Key} Value : {item.Value}");
        }
    }
}
item : [language, C#] 
	 Key language Value : C#
item : [topic, Dictionary] 
	 Key topic Value : Dictionary
item : [label, Beginner] 
	 Key label Value : Beginner
III
using System.IO;
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Dictionary < string,string> dict = new Dictionary<string,string> {
            {"language","C#"},{"topic","Dictionary"},{"label","Beginner"}
        };
        Console.WriteLine("Iterate Over Keys");
        foreach( var key   in dict.Keys) {
            Console.WriteLine( $"Key : {key}");
        }
        Console.WriteLine("Iterate Over Values");
        foreach( var value   in dict.Values) {
            Console.WriteLine( $"Value : {value}");
        }
    }
}
Iterate Over Keys
Key : language
Key : topic
Key : label
Iterate Over Values
Value : C#
Value : Dictionary
Value : Beginner

Using For Loop

I : with the help of Linq
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        Dictionary < string,string> dict = new Dictionary<string,string> {
            {"language","C#"},{"topic","Dictionary"},{"label","Beginner"}
        };
        for(int i=0; i<dict.Count; i++) {
            var  item = dict.ElementAt(i); // Or KeyValuePair<string , string> item  = dict.ElementAt(i);
            Console.WriteLine("Key : "+item.Key+" Value : "+item.Value);
        }
    }
}
Key : language Value : C#
Key : topic Value : Dictionary
Key : label Value : Beginner

Using ParallelEnumerable.ForAll Method

This method invokes in parallel the specified action for each element in the source and It is an efficient way to iterate a large Dictionary

Signature of ForAll :-
public static void ForAll<TSource> (this System.Linq.ParallelQuery<TSource> source, Action<TSource> action);
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        Dictionary < string,string> dict = new Dictionary<string,string> {
            {"language","C#"},{"topic","Dictionary"},{"label","Beginner"}
        };
        dict.AsParallel().ForAll(d => Console.WriteLine("item "+d+" Key : "+d.Key+" Value : "+d.Value));
    }
}
item [language, C#] Key : language Value : C#
item [label, Beginner] Key : label Value : Beginner
item [topic, Dictionary] Key : topic Value : Dictionary

String.Join Method

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Dictionary < string,string> dict = new Dictionary<string,string> {
            {"language","C#"},{"topic","Dictionary"},{"label","Beginner"}
        };
        Console.WriteLine(String.Join(",", dict));
        Console.WriteLine(String.Join(",", dict.Keys));
        Console.WriteLine(String.Join(",", dict.Values));
    }
}
[language, C#],[topic, Dictionary],[label, Beginner]
language,topic,label
C#,Dictionary,Beginner