Srini
Srini

Reputation: 126

What is the best .NET type for collections where each key can have multiple values?

I have one key but different values for that key. I tried HashTable but I don't think that is the right way of doing it.

I'll explain in detail what the requirement is:

I have a Key - "Asp.net" - for that I have n number of values - 61, 67, 100

So I have to store this somewhere like Dictionary or hash table.

Upvotes: 4

Views: 1701

Answers (4)

Rohit
Rohit

Reputation: 3638

If both your key and values are string you may want to use NameValueCollection.

NameValueCollection class stores multiple string values under a single key. Acording to MSDN, this class can be used for headers, query strings and form data.

Upvotes: 2

Eric Lippert
Eric Lippert

Reputation: 659964

I think you want PowerCollections MultiDictionary.

http://www.codeplex.com/PowerCollections

Upvotes: 3

Adam Lear
Adam Lear

Reputation: 38758

You could use a Dictionary<string, List<int>>:

var dictionary = new Dictionary<string, List<int>>();
dictionary.Add("asp.net", new List<int>());
dictionary["asp.net"].Add(61);
dictionary["asp.net"].Add(67);
dictionary["asp.net"].Add(100);

Upvotes: 9

Adam Robinson
Adam Robinson

Reputation: 185593

A Dictionary<string, List<int>> would do the trick for your sample data.

Upvotes: 10

Related Questions