How To Convert Dictionary To List In C#

Okay, so picture this: I'm building this app, right? And I've got this dictionary holding user data – names paired with their ages. Super straightforward. Then, BAM! The UI designer drops a bomb: "We need this data in a list, not a dictionary." My initial reaction? Internally screaming emoji. Why? Well, because why not make things needlessly complex, right? But hey, that’s software development. Learning how to adapt is key.
Turns out, converting a dictionary to a list in C# isn't the soul-crushing task I initially feared. In fact, it's surprisingly simple. So, let's dive in, shall we?
The Basic Idea
At its core, a dictionary in C# is a collection of key-value pairs. Think of it like a real-world dictionary, where you have a word (the key) and its definition (the value). A list, on the other hand, is just a sequentially ordered collection of items. So, we basically need to take those key-value pairs and somehow represent them as elements within a list. There are a few ways to accomplish this, depending on what exactly you want in your list.
Must Read
We've got options! And who doesn't love options? (Besides that UI designer, apparently.)
Option 1: List of KeyValuePair Objects
The most straightforward approach is to create a list of KeyValuePair<TKey, TValue> objects. This preserves both the key and the value for each entry. In C#, KeyValuePair is a struct designed specifically for holding key-value pairs.
Here's the code:

Dictionary<string, int> myDictionary = new Dictionary<string, int>()
{
{"Alice", 30},
{"Bob", 25},
{"Charlie", 35}
};
List<KeyValuePair<string, int>> myList = myDictionary.ToList();
Bam! Done. Seriously, that's it. The ToList() method, part of the System.Linq namespace, extension methods handles all the heavy lifting. Just make sure you have using System.Linq; at the top of your file. (You probably already do. Everyone loves LINQ.)
Now myList contains KeyValuePair objects. You can access the key and value of each item like this:
foreach (KeyValuePair<string, int> item in myList)
{
Console.WriteLine($"Name: {item.Key}, Age: {item.Value}");
}
Pretty neat, huh?

Option 2: List of Keys or Values Only
Maybe you only need the keys or the values. In that case, you can extract them using the Keys and Values properties of the dictionary, respectively.
For example, to get a list of names (keys):
List<string> nameList = myDictionary.Keys.ToList();
And to get a list of ages (values):

List<int> ageList = myDictionary.Values.ToList();
Again, super simple! Just access the Keys or Values property and then call ToList() on the resulting collection. The Keys and Values properties return IEnumerable<TKey> and IEnumerable<TValue>, respectively, which are readily converted to lists using LINQ's ToList().
Option 3: List of Custom Objects
Okay, so let’s say you don’t want KeyValuePair objects. Maybe you have a custom class or struct that you want to populate with the key and value from each dictionary entry. No problem! This requires a little more code, but it's still pretty straightforward.
First, define your custom class:

public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Then, use LINQ's Select method to project each dictionary entry into a Person object:
List<Person> personList = myDictionary
.Select(kvp => new Person { Name = kvp.Key, Age = kvp.Value })
.ToList();
Here, the Select method iterates through each key-value pair (kvp). For each pair, it creates a new Person object, setting the Name property to the key and the Age property to the value. Finally, ToList() converts the resulting collection of Person objects into a list.
This approach gives you complete control over how the dictionary data is transformed into the list. Pretty cool, eh? I thought so.
In Conclusion
Converting a dictionary to a list in C# is a common task, and as you've seen, there are several ways to do it. The best approach depends on your specific needs and what kind of data you want in your list. Whether you need a list of KeyValuePair objects, just the keys or values, or a list of custom objects, C# and LINQ provide the tools you need to get the job done. And remember, don't be afraid of those UI designers. They might throw curveballs, but you've got this!
