89

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

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

Sam
  • 40,644
  • 36
  • 176
  • 219
user256034
  • 4,289
  • 5
  • 37
  • 44
  • 6
    "Multi-map" support is the first thing that comes to mind (one key can map to a set of values). –  Mar 25 '11 at 18:09
  • Possible Duplicate of "[What is the point of Lookup?](https://stackoverflow.com/q/1403493/107625)". – Uwe Keim Feb 06 '18 at 07:48

3 Answers3

129

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.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • 1
    Indeed, each entry is an `IEnumerable` instead of a `T`. – Gabe Mar 25 '11 at 18:11
  • 1
    +1 While true, it would be nice to have more on difference with interface, update-mutability, key requirements, etc. –  Mar 25 '11 at 18:11
  • Implementing `ILookup` 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` 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` should imply that a list is immutable. – binki Nov 26 '15 at 20:35
  • 1
    @binki Exposing a mutable implementation of an ```ILookup``` 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``` 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 Feb 04 '16 at 02:42
  • 1
    Grammatical error in my comment: I mean that your consumers should be able to assume you followed the principle of least astonishment. – Aluan Haddad Feb 04 '16 at 02:51
  • 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.IsReadOnly`](https://msdn.microsoft.com/en-us/library/0cfatk9t(v=vs.110).aspx) existing `IDictionary` is not necessarily mutable ;-). Sorry for the overly pedantic criticism xD – binki Feb 04 '16 at 03:15
4

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

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • Do you mean that ILookup is a read-only subset of an IDictionary? – Timothy Swan Jan 14 '15 at 19:08
  • 1
    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. – Philippe Apr 14 '15 at 07:35
  • 2
    @Philippe The read only interface to the dictionary is `IReadOnlyDictionary` and all instances of the `Dictionary` class implement it (`ToDictionary` returns the class instance, so it works too), so you can expose any dictionary as read only. – Los Frijoles Jun 01 '15 at 23:53
  • 1
    @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. – Philippe Jun 02 '15 at 06:37
2

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

Magesh
  • 51
  • 1
  • 3