The MSDN explains Lookup like this:
A
Lookup<TKey, TElement>
resembles aDictionary<TKey,. The difference is that a
TValue>
Dictionary<TKey, TValue> maps keys to single values, whereas a
Lookup<TKey, TElement> maps keys to collections of values.
I don’t find that explanation particularly helpful. What is Lookup used for?
It’s a cross between an
IGroupingand a dictionary. It lets you group items together by a key, but then access them via that key in an efficient manner (rather than just iterating over them all, which is whatGroupBylets you do).For example, you could take a load of .NET types and build a lookup by namespace… then get to all the types in a particular namespace very easily:
(I’d normally use
varfor most of these declarations, in normal code.)