C# List to Dictionary and Dictionary to List: Conversion Guide with Examples

C# List to Dictionary and Dictionary to List: Conversion Guide with Examples

List to Dictionary conversion in C# is the process of transforming a List into a Dictionary<TKey, TValue>, while inverse conversion transforms a dictionary back into a list.

In C#, both List and Dictionary are commonly used collections but serve different purposes. A List stores ordered elements, while a Dictionary stores key-value pairs for fast lookup. Converting between them is useful when you need fast search capabilities or when you want to flatten structured data. LINQ provides simple methods like ToDictionary() and ToList() to perform these conversions efficiently.

When are These Conversions Needed?

Use List to Dictionary when:

• You need fast lookup by key
• You want to eliminate duplicates using unique keys
• You are optimizing search operations

Use Dictionary to List when:

• You need ordered iteration
• You want to display data in UI
• You are preparing data for serialization or processing

How to Convert List to Dictionary?

Basic Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        List<string> names = new List<string> { "Alice", "Bob", "Charlie" };

        Dictionary<int, string> dict = names
            .Select((name, index) => new { index, name })
            .ToDictionary(x => x.index, x => x.name);

        foreach (var item in dict)
        {
            Console.WriteLine($"{item.Key} - {item.Value}");
        }
    }
}

List of Objects to Dictionary

using System;
using System.Collections.Generic;
using System.Linq;

class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

class Program
{
    static void Main()
    {
        List<Person> people = new List<Person>
        {
            new Person { Id = 1, Name = "Alice" },
            new Person { Id = 2, Name = "Bob" }
        };

        Dictionary<int, string> dict = people.ToDictionary(p => p.Id, p => p.Name);

        foreach (var item in dict)
        {
            Console.WriteLine($"{item.Key} - {item.Value}");
        }
    }
}

How to Convert Dictionary to List

Convert to List of KeyValuePair

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        Dictionary<int, string> dict = new Dictionary<int, string>
        {
            { 1, "Alice" },
            { 2, "Bob" }
        };

        List<KeyValuePair<int, string>> list = dict.ToList();

        foreach (var item in list)
        {
            Console.WriteLine($"{item.Key} - {item.Value}");
        }
    }
}

Convert Dictionary to List of Values

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        Dictionary<int, string> dict = new Dictionary<int, string>
        {
            { 1, "Alice" },
            { 2, "Bob" }
        };

        List<string> values = dict.Values.ToList();

        foreach (var value in values)
        {
            Console.WriteLine(value);
        }
    }
}

Conversion Comparison

Conversion Method Result Type
List → Dictionary ToDictionary() Key-Value pairs
Dictionary → List ToList() List of KeyValuePair
Dictionary → Values List Values.ToList() List of values only
Dictionary → Keys List Keys.ToList() List of keys only

Advantages and Disadvantages

Conversion Advantages Disadvantages
List to Dictionary Fast lookup, structured data access Requires unique keys
Dictionary to List Easy iteration and display Loses key-based access speed

Similar and Alternative Options

Option Description When to Use
Lookup Read-only key grouping Grouping data efficiently
HashSet Unique element collection Removing duplicates
LINQ GroupBy Groups data by key Complex transformations
Custom mapping Manual conversion logic Special requirements

Common Mistakes

• Using non-unique keys in ToDictionary()
• Ignoring duplicate key exceptions
• Converting Dictionary to List and expecting key-based access
• Overusing conversions instead of choosing correct structure
• Forgetting to use .Values or .Keys when needed
• Not handling null collections before conversion

Contents related to 'C# List to Dictionary and Dictionary to List: Conversion Guide with Examples'

C# Async/Await Tutorial: How to Use Asynchronous Programming in C#
C# Async/Await Tutorial: How to Use Asynchronous Programming in C#
C# var vs dynamic: Differences, Usage, and Best Practices
C# var vs dynamic: Differences, Usage, and Best Practices