About System.Linq.Lookup class

拈花ヽ惹草 提交于 2021-02-07 05:03:34

问题


I came across this class while reading a C# book and have some questions.

  • Why is this added into System.Linq namespace and not into usuall Collections namespace?
  • What the intention behind this class is
  • Why this class is not intended for direct instantiation? This is available through the ToLookup extension only, right?

回答1:


Purpose of the class: a dictionary where a key can map to multiple values. Think of it as being for grouping rather than one-to-one mapping.

Only through ToLookup decision: Pass. Again, seems like a bad call to me. On the other hand, it means that the result is immutable to the outside world, which is quite nice. It's quite easy to write your own collection which supports this, of course - but it would be have been quite nice to have it in the collections "properly". My guess is that MS didn't have the time/money to go through the pretty rigorous design/test required to make it a first class collections decision.

Namespace decision: Probably related to the above. Having a version in System.Collections.Generic which you couldn't create yourself would have been a bit odd.




回答2:


As an aside, note that MiscUtil also includes a MiscUtil.Linq.EditableLookup<,> class, that is similar; it implements the regular ILookup<,> interface, but is fully mutable - so you can create it and add your own values.



来源:https://stackoverflow.com/questions/197241/about-system-linq-lookup-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!