ILookup interface vs IDictionary

C#.Net

C# Problem Overview


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

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

C# Solutions


Solution 1 - C#

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 - C#

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 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser256034View Question on Stackoverflow
Solution 1 - C#BrokenGlassView Answer on Stackoverflow
Solution 2 - C#Daniel A. WhiteView Answer on Stackoverflow
Solution 3 - C#MageshView Answer on Stackoverflow