ILookup interface vs IDictionary

后端 未结 2 1492
执念已碎
执念已碎 2021-02-03 16:29

How does the ILookup interface differ from IDictionary?

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

相关标签:
2条回答
  • 2021-02-03 17:12

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

    0 讨论(0)
  • 2021-02-03 17:18

    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.

    0 讨论(0)
提交回复
热议问题