I’m trying to wrap my head around which data structures are the most efficient and when / where to use which ones.
Now, it could be that I simply just don’t understand the structures well enough, but how is an ILookup(of key, ...) different from a Dictionary(of key, list(of ...))?
Also where would I want to use an ILookup and where would it be more efficient in terms of program speed / memory / data accessing, etc?
Two significant differences:
Lookupis immutable. Yay 🙂 (At least, I believe the concreteLookupclass is immutable, and theILookupinterface doesn’t provide any mutating members. There could be other mutable implementations, of course.)KeyNotFoundException. (Hence there’s noTryGetValue, AFAICR.)They’re likely to be equivalent in efficiency – the lookup may well use a
Dictionary<TKey, GroupingImplementation<TValue>>behind the scenes, for example. Choose between them based on your requirements. Personally I find that the lookup is usually a better fit than aDictionary<TKey, List<TValue>>, mostly due to the first two points above.Note that as an implementation detail, the concrete implementation of
IGrouping<,>which is used for the values implementsIList<TValue>, which means that it’s efficient to use withCount(),ElementAt()etc.