Home
Manage Your Code
Snippet: Iterates contents of a MultiDictionary (C#)
Title: Iterates contents of a MultiDictionary Language: C#
Description: Iterates contents of Wintellect.PowerCollections.MultiDictionary instance. Views: 2450
Author: Kevin McFarlane Date Added: 12/21/2007
Copy Code  
1using Wintellect.PowerCollections;
2
3// ...

4
5public static void MultiDictionaryExample()
6{
7    // Initialise

8    bool allowDuplicateValues = true;
9    MultiDictionary<int, int> dict = new MultiDictionary<int, int>(allowDuplicateValues);
10    dict.AddMany(1, new int[] { 4, 4, 6 });
11    dict.AddMany(2, new int[] { 40, 3, 6 });
12    dict.AddMany(3, new int[] { 42, 15, 8 });
13
14    // Print

15    dict.ForEach(delegate(KeyValuePair<int, ICollection<int>> kvp)
16        {
17            Console.WriteLine("Key: " + kvp.Key);
18            Console.Write("Values: ");
19
20            foreach (int var in kvp.Value)
21            {
22                Console.Write(var + " ");
23            }
24            Console.Write(Environment.NewLine);
25        });
26}
27//Results:

28
29//Key: 1

30//Values: 4 4 6

31//Key: 3

32//Values: 42 15 8

33//Key: 2

34//Values: 40 3 6

35
Usage
Download PowerCollections from codeplex
http://www.codeplex.com/PowerCollections

Add reference to PowerCollections dll.
Insert using statement as above.