tags:

views:

1203

answers:

2

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?
+9  A: 

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.

Jon Skeet
+1  A: 

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.

Marc Gravell