Dictionary<TKey, TValue>
The .NET collection that maps unique keys to values and finds a value by its key in constant time by hashing the key.
In more detail
Reading a missing key with the indexer throws, so use ContainsKey or TryGetValue when absence is normal. Keys must not change their hash code while stored.
Example
var prices = new Dictionary<string, decimal> { ["Kettle"] = 24.50m };Related terms
- collection — An object that holds a group of items, such as a list, dictionary, set or array.
- HashSet<T> — The .NET collection of unique items that answers 'have I seen this one already?' in constant time.
- GetHashCode — The method that returns a number summarising an object, used by hash-based collections such as Dictionary and HashSet to find it quickly.
- time complexity — A description of how the amount of work an algorithm does grows as its input gets bigger.
Taught in
- Collections, Generics, and LINQ — Modern .NET Core, C# & Enterprise Microservices
- Records, Pattern Matching, and Modern C# — Modern .NET Core, C# & Enterprise Microservices