Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. Enumerable.ToList() / ToArray() / ToDictionary()

Enumerable.ToList() / ToArray() / ToDictionary()

Extension methods that convert a LINQ query result into a concrete collection type. They materialize a lazily evaluated sequence into a reusable form.

Syntax

using System.Linq;

// Converts to List<T> (supports adding and removing elements).
List<T> list = source.ToList();

// Converts to an array (fixed length).
T[] array = source.ToArray();

// Converts to Dictionary<TKey, TValue>.
Dictionary<TKey, TValue> dict = source.ToDictionary(x => key, x => value);

// Converts to HashSet<T> (duplicates are removed automatically).
HashSet<T> set = source.ToHashSet();

Method List

MethodDescription
ToList()Eagerly evaluates the sequence and returns a List<T>.
ToArray()Eagerly evaluates the sequence and returns an array.
ToDictionary(keySelector)Builds a dictionary using the key selector. The value is the element itself.
ToDictionary(keySelector, elementSelector)Builds a dictionary with separate selectors for keys and values.
ToHashSet()Returns a HashSet<T> with duplicates removed automatically.
ToLookup(keySelector)Returns a Lookup that maps one key to multiple values (see GroupBy for details).

Sample Code

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

IEnumerable<int> source = Enumerable.Range(1, 5);

// ToList: converts to List<T>.
List<int> list = source.ToList();
list.Add(6);
Console.WriteLine(string.Join(", ", list)); // 1, 2, 3, 4, 5, 6

// ToArray: converts to an array.
int[] array = source.ToArray();
Console.WriteLine(array.Length); // 5

// ToDictionary: builds a dictionary keyed by ID.
var users = new List<(int Id, string Name)>
{
    (1, "Alice"),
    (2, "Bob"),
    (3, "Carol"),
};
Dictionary<int, string> dict = users.ToDictionary(u => u.Id, u => u.Name);
Console.WriteLine(dict[2]); // Bob

// ToHashSet: removes duplicates automatically.
List<int> withDups = new List<int> { 1, 2, 2, 3, 3, 3 };
HashSet<int> set = withDups.ToHashSet();
Console.WriteLine(string.Join(", ", set)); // 1, 2, 3 (order may vary)
Console.WriteLine(set.Contains(2));        // true

// Convert LINQ results to a dictionary for fast lookups.
var products = new List<(string Code, string Name, int Price)>
{
    ("A001", "Apple",  150),
    ("A002", "Orange",  80),
    ("B001", "Carrot", 100),
};
var priceByCode = products.ToDictionary(p => p.Code, p => p.Price);
Console.WriteLine(priceByCode["A001"]); // 150

Notes

Because LINQ methods use deferred execution, the query is re-evaluated each time you enumerate the sequence. Calling ToList() or ToArray() forces immediate evaluation and loads the results into memory, which is useful when you need to iterate multiple times or when the query is expensive to compute.

ToDictionary() throws an ArgumentException if the key selector produces duplicate keys. If duplicates are possible, remove them first with DistinctBy(), or use ToLookup() which allows multiple values per key.

To filter the source sequence before converting it, see Enumerable.Where(). To remove duplicates without converting to a dictionary, see Enumerable.Distinct().

If you find any errors or copyright issues, please .