ILookup interface vs IDictionary

21,969

Solution 1

ILookup entries can contain multiple items per key - each key is mapped to an IEnumerable<TElement>.

Also as hinted to in the comments an ILookup is immutable, while you can update values in an IDictionary (it exposes an Add() method and an indexer that allows getting and setting values).

In summary their use case is very different - you use a lookup when you need a 1:N map with values that are fixed and won't (and can't) change. A dictionary on the other hand offers a mutable 1:1 mapping of key value pairs, so it can be updated to add or remove values.

Solution 2

It is much more simpler than IDictionary. It is used by Linq. It only has Contains, Item and Count. IDictionary has Add, Remove, etc.

Solution 3

ILookUp => Group by key , Enumerable Collection

Single key value refers to enumerable collection where we can iterate through the value collection.

IDictionary => Group by distinct key , Single value

Share:
21,969

Related videos on Youtube

user256034
Author by

user256034

Updated on July 22, 2021

Comments

  • user256034
    user256034 almost 2 years

    How does the ILookup<key, value> interface differ from IDictionary<key, value>?

    I don't understand what the ILookup interface is meant for.

  • Gabe
    Gabe about 12 years
    Indeed, each entry is an IEnumerable<T> instead of a T.
  • Admin
    Admin about 12 years
    +1 While true, it would be nice to have more on difference with interface, update-mutability, key requirements, etc.
  • Timothy Swan
    Timothy Swan over 8 years
    Do you mean that ILookup is a read-only subset of an IDictionary?
  • Philippe
    Philippe about 8 years
    Kind of... But each element is a IGrouping, so there can be multiple elements for one key so it does not map well. I would have been nice to have a read only interface to Dictionary.
  • Los Frijoles
    Los Frijoles almost 8 years
    @Philippe The read only interface to the dictionary is IReadOnlyDictionary<TKey, TValue> and all instances of the Dictionary<TKey, TValue> class implement it (ToDictionary returns the class instance, so it works too), so you can expose any dictionary as read only.
  • Philippe
    Philippe almost 8 years
    @LosFrijoles Thanks, I didn't know this one existed. I'm currently stuck with .NET 4.0 and incorrectly assumed that it didn't exist without checking on MSDN.
  • binki
    binki over 7 years
    Implementing ILookup<TElement> is no guarantee of immutability. Your answer seems to imply that it is. It merely provides a readonly view of a lookup. Also, how a method that accepts an ILookup<TElement> might handle the lookup’s content changing is up to that method—and if there is no documentation it’s safer to avoid mutating a lookup while foreign code is still referencing it and depending on your use case. See the whole discussion on how people wrongly think that implementing IReadOnlyList<T> should imply that a list is immutable.
  • Aluan Haddad
    Aluan Haddad over 7 years
    @binki Exposing a mutable implementation of an ILookup<TKey, TElement> it's a bad idea if you plan to mutate it. If there are no docs, you should be able to assume that it abides by its interface. Of course real life is messy, but if you create a lookup with one of the ToLookup extension methods, it should be safe to assume it will not be modified. There are ways in which you could, but consumers of your ILookup<TKey, TElement> typed members should follow the principle of least astonishment. If you find you have been passed one which mutates in an observable way, throw.
  • Aluan Haddad
    Aluan Haddad over 7 years
    Grammatical error in my comment: I mean that your consumers should be able to assume you followed the principle of least astonishment.
  • binki
    binki over 7 years
    Yeah. It's a bad idea to mutate an object you passed to a method which accepts a read-only view such as IReadOnlyDictionary or ILookup. I'm just trying to make the distinction between “read-only view” and “immutable”. I would also like to note that, with IDictionary<TKey, TValue>.IsReadOnly existing IDictionary is not necessarily mutable ;-). Sorry for the overly pedantic criticism xD