What is a dictionary?

A dictionary consists of a key and a value. In an array, the key is created automatically (0, 1, and so on). With dictionaries, the key name can be set to something meaningful. As with lists, dictionaries use generics.

To use a dictionary, add the following line of code:

using System.Collections.Generic; // C# 10 comes with ImplicitUsings, meaning the most popular using namespaces such as this one is already built-in so there is no need to declare it

Example: Instantiate and assign items to a dictionary

Dictionaries have two generic data type parameters. Because they’re generic, any data type can be entered. In the example below, the first generic data type is a string and is used as the key. The second generic data type is an int, and string is used for the value.

Get hands-on with 1200+ tech skills courses.