C-SHARP - 4.5 Dictionary
This video introduces dictionaries in C#, a key collection type to master. Unlike arrays and lists, a dictionary does not use indexes to access elements; it uses keys. Each entry is a pair (key, value): the key is a unique label, the value is the data attached to it. To declare one we use var dico = new Dictionary<string, string>();, choosing the type of the key and the type of the value (string, int, bool, etc.).
To add an entry we call dico.Add(key, value), or we use the bracket syntax dico[key] = value. To read a value we access it by key: dico["fruit"]. If the key does not exist, the program throws an exception. To handle this we use dico.ContainsKey(key), which returns a boolean and lets us guard the access with an if.
var dico = new Dictionary<string, string>();
dico.Add("fruit", "Peach");
dico["vegetable"] = "Carrot";
Console.WriteLine("What are you looking for?");
string search = Console.ReadLine().ToLower();
if (dico.ContainsKey(search))
{
Console.WriteLine(dico[search]);
}
else
{
Console.WriteLine("The key you typed is not in the dictionary.");
}
Why dictionaries are powerful
- Direct access to a value by its key: O(1) lookup, no loop required.
- Perfect for big data sets: millions of records can be searched instantly by key.
- With a list you would loop until you find the right item, which is far slower.
- Use a dictionary whenever you need fast searches on a unique identifier.
To compare: storing names and ages in a list of string[] arrays forces us to loop until we find the right name. A dictionary with the name as key gives the age in a single instruction. As soon as you handle large amounts of data and need to look them up, think dictionaries; they will completely change your program's performance on web, mobile or backend applications.
Summary
This lesson covers C# dictionaries, a data structure that uses key-value pairs instead of index-based access like arrays or lists. Dictionaries are created using the Dictionary<T,U> syntax and can store any data types for keys and values. The lesson demonstrates how to add elements, retrieve values by key, check key existence using ContainsKey(), and compares the search efficiency of dictionaries versus lists.
Key points
- Dictionaries use key-value pairs for accessing elements instead of numeric indices
- Dictionary<KeyType, ValueType> syntax allows specifying data types for both keys and values
- Elements are added either with the .Add() method or bracket notation (dictionary[key] = value)
- The ContainsKey() method verifies a key exists before accessing to prevent exceptions
- Direct key-based access provides fast lookups, especially compared to iterating through lists
- Dictionaries are ideal for storing related data like names paired with ages or coordinates
FAQ
How do dictionaries differ from lists in C#?
Dictionaries use key-value pairs for access instead of numeric indices, making them ideal for searching by meaningful identifiers such as names or IDs rather than position-based lookups.
What happens if you try to access a key that doesn't exist in a dictionary?
An exception is thrown. You can prevent this by using the ContainsKey() method to verify the key exists before accessing its value.
What data types can be used for keys and values in a dictionary?
Both keys and values can be of any data type (string, int, bool, etc.), specified using the Dictionary<KeyType, ValueType> syntax when the dictionary is declared.