开发者

.NET collection with repeating keys and values?

I'm looking for a dictionary type .NET c开发者_如何学运维ollection supported a repeating keys and values, something like this

Collection c = new Collection();
c["Key"] = "value";
c["Key"] = "value";
c["Key1"] = "value1";

Dictionary doesn't allow the same keys


I would suggest to use the class Lookup<TKey,TElement>. It acts like a dictionary that allows duplicate keys.


You could use a dictionary containing a list:

Dictionary<string, List<string>> MyValues;


If you need something like multimap in C++, there is no direct equivalent in .NET. But you can emulate this by putting a collection as a value into Dictionary of TKey, TValue:

Dictionary<string, List<string>> c = new Dictionary<string, List<string>>();
c["Key"] = new List<string>();
c["Key"].Add("value");
c["Key"].Add("value2");

c["Key2"] = new List<string>();
c["Key2"].Add("Value1");

Another way - is to create your own collection that will "hide" all unncesessary operations inside some helper methods. For example, your indexer could check whether you already have apporpriate key in your hashtable and create empty list inside.


You´re looking for an Dictionary


Hashtable?

http://msdn.microsoft.com/de-de/library/system.collections.hashtable.aspx


A multimap should be sufficient.


Just a guess but why not use the the dictionary but make the value a List?

Dictionary<string, List<string>> c = new Dictionary<string, List<string>>();
c["Key"] = new List<string>();
c["Key1"] = new List<string>();
c["Key"].Add("value");
c["Key"].Add("value");
c["Key1"].Add("value1");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜