Convert Dictionary to List Collection in C#

Convert Dictionary to List Collection in C#

In C#, a dictionary can be converted into a list using the ToList() method as a part of the System.Linq extensions. A dictionary cannot be converted to a List<string> directly because the return type of dictionary is KeyCollection.

The list is similar to an ArrayList, the only difference being that the list is generic and has unique properties.

This tutorial will teach you how to convert a dictionary to a list collection in C#.

Use the ToList() Method to Convert Dictionary to List Collection in C#

A dictionary in C# accepts two arguments, the first one is the key and the second one is the value. The dictionary is useful when adding different data and their values to a list.

To convert dictionary values into a list collection, we can convert this generic list by its built-in ToList() method. Both lists and dictionaries store data collections and are similar; both are random access data structures of the .NET framework.

The dictionary is based on a hash table, an efficient algorithm for looking up things. On the other hand, a list goes element by element until it finds the result from beginning to the result each time.

It’s easy to convert a dictionary into a list collection in C# using the System.Collections.Generic and System.Linq namespaces. Call ToList() on a dictionary collection, yielding a list of KeyValuePair instances.

// add `System.Collections.Generic` and `System.Linq` namespaces
using System;
using System.Collections.Generic;
using System.Linq;

class ConDtoLCol
{
    static void Main()
    {
        // create a Dictionary
        Dictionary<string, int> dict = new Dictionary<string, int>();
        dict["cat"] = 1;
        dict["dog"] = 4;
        dict["mouse"] = 2;
        dict["rabbit"] = -1;

        // call ToList to convert the `dict` Dictionary to List
        List<KeyValuePair<string, int>> list = dict.ToList();

        // loop over List to show successful conversion
        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

If you want to convert the keys of a dictionary to a list collection in C#, you can use:

List<string> listNumber = dicNumber.Keys.ToList();

Furthermore, to convert values of a dictionary to a list collection in C#, you can use:

List<string> listNumber = dicNumber.Values.ToList();

The .NET framework dictionary represents a collection of keys and values. The primary purpose of dictionary keys is to allow users to get a generic list (collection) of the dictionary values.

Furthermore, you can use the values property to get that generic list of dictionary values. In this article, you have learned the most efficient way to convert keys and values of a dictionary to a list in C#.

Syed Hassan Sabeeh Kazmi avatar Syed Hassan Sabeeh Kazmi avatar

Hassan is a Software Engineer with a well-developed set of programming skills. He uses his knowledge and writing capabilities to produce interesting-to-read technical articles.

GitHub

Related Article - Csharp Dictionary

Related Article - Csharp List