
How can you convert your Dictionary collection into a List collection in your C# program? A Dictionary can easily be converted to a List of pairs with the ToList extension method.
These C# programs convert Dictionary and List instances. They use KeyValuePair.
To start, this program includes the System.Collections.Generic and System.Linq namespaces to allow easy access to the needed types. We create and populate a Dictionary collection. Next, we call ToList() on that Dictionary collection, yielding a List of KeyValuePair instances. Finally, we loop over the List instance and print all the Key and Value properties.
KeyValuePair HintsProgram that calls ToList on Dictionary [C#]
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
Dictionary<string, int> dictionary = new Dictionary<string, int>();
dictionary["cat"] = 1;
dictionary["dog"] = 4;
dictionary["mouse"] = 2;
dictionary["rabbit"] = -1;
// Call ToList.
List<KeyValuePair<string, int>> list = dictionary.ToList();
// Loop over list.
foreach (KeyValuePair<string, int> pair in list)
{
Console.WriteLine(pair.Key);
Console.WriteLine(" {0}", pair.Value);
}
}
}
Output
cat
1
dog
4
mouse
2
rabbit
-1
How it works. The ToList extension method acts upon an IEnumerable generic collection. The Dictionary collection implements the IEnumerable interface with type parameter KeyValuePair<K, V>. Thus, the Dictionary's implementation allows the ToList extension method to work here.
ToList Extension Method IEnumerable
Why would you ever want to convert your Dictionary to a List? First, there may be other methods in your program that require a List not a Dictionary. Second, the List has some superior performance characteristics: it is faster to loop over and requires less memory due to its lack of an internal buckets structure.
Dictionary Versus List Lookup TimeHere we see how you can populate a Dictionary with the values in your List. Look closely how you can use ContainsKey in the loop in which you add the elements to the Dictionary. This avoids the chance of an exception being raised.
ContainsKey MethodProgram that converts List [C#]
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Create new List. [1]
List<string> list = new List<string>();
list.Add("Olympics");
list.Add("Nascar");
list.Add("Super Bowl");
list.Add("Wimbledon");
// Put List values into Dictionary. [2]
var exampleDictionary = new Dictionary<string, int>();
foreach (string value in list)
{
if (!exampleDictionary.ContainsKey(value))
{
exampleDictionary.Add(value, 1);
}
}
// Display Dictionary. [3]
foreach (var pair in exampleDictionary)
{
Console.WriteLine(pair);
}
}
}
Output
[Olympics, 1]
[Nascar, 1]
[Super Bowl, 1]
[Wimbledon, 1]
Part 1. The first part of the example populates the List with four arbitrary strings. The List then has four elements, all of which we need to add to the Dictionary next.
Part 2. Here we declare a new Dictionary that has a key type of string, which is the same type as the List. Having the types match is important. We loop through the List and then call ContainsKey and Add. We only call Add if ContainsKey returns false.
Part 3. This part of the example simply displays the contents of the Dictionary we created and populated. You can see how the strings from the List are the new keys.
Console.WriteLineOften you may need to obtain a List of the keys in a Dictionary, which we see in this example. Also shown here is how to get a List of the values, although this requirement is much less common in my experience.
Program that gets Keys and Values [C#]
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Popular example Dictionary. [1]
Dictionary<string, int> birdDictionary = new Dictionary<string, int>();
birdDictionary.Add("parakeet", 3);
birdDictionary.Add("parrot", 5);
birdDictionary.Add("finch", 7);
birdDictionary.Add("lovebird", 9);
// Get List of keys. [2]
List<string> keyList = new List<string>(birdDictionary.Keys);
// Display them.
foreach (var value in keyList)
{
Console.WriteLine(value);
}
// Get List of values. [3]
List<int> valueList = new List<int>(birdDictionary.Values);
// Display them.
foreach (var value in valueList)
{
Console.WriteLine(value);
}
}
}
Output
parakeet
parrot
finch
lovebird
3
5
7
9
Part 1. Here we fill the Dictionary by calling the Add method with two parameters. This Dictionary happens to contain the types of pet birds.
Part 2. Here we use the new List constructor on the Keys collection from the Dictionary of bird names. The List constructor accepts an IEnumerable collection with an element type of string. If your List has another value type, the element type will be different.
List ExamplesPart 3. This part is the same as the previous part except it uses the List constructor with a parameter of the Values collection.

We looked at an easy way to convert a Dictionary into a List of KeyValuePair struct values. We further explored the interaction between the ToList method and the Dictionary type's implementation of IEnumerable. Further, we saw a way you can put the elements in your List into a Dictionary, and ways you can put the elements of a Dictionary in a List.
Cast Examples