Search code examples
.netlookupnamevaluecollection

namevaluecollection vs lookup


I need to represent some data that has a key and one or more values, seems there is a namevaluecollection or a lookup.

From what I can tell the main difference between the two is that lookup is immutable, is there any other significant differences in the two I should be aware of?


Solution

  • NameValueCollection and ILookup are different in several ways.

    First, NameValueCollection is a map from string -> string - each key can only have one value, and both the key and the value must be strings. This doesn't sound like it meets your requirement:

    I need to represent some data that has a key and one or more values

    Second, ILookup is essentially a generic multimap - it maps a key to a set of one or more values - and both the key and values can be of any type. ILookup<,> is immutable, as you mention, but it's also not directly constructable. The only way to get an instance is to use the Enumerable.ToLookup() method. This requires that all of the data must be available at once - you can't build the multimap over time.

    If what you really need is a mutable multimap, you should look at either the EditableLookup<,> in MiscUtil, your wrap a Dictionary<TKey,List<TValue>> in your own type.