Junior Collections

What is a Dictionary<TKey, TValue> in C#?

A Dictionary<TKey, TValue> is a generic collection in C# that stores data as

key-value pairs. It provides fast lookup, addition, and removal of values based on their

keys.

  • Keys must be unique
  • Keys must be non-null (for reference types)
  • Values can be null if the type allows it

Example:

Dictionary<string, int> ages = new Dictionary<string, int>();

ages.Add("Alice", 30);

ages.Add("Bob", 25);

Use case: Storing user profiles by ID, or mapping product names to prices.

More from C# Programming Tutorial

All questions for this course