多值字典?

任何人都知道a的良好实现MultiValueDictionary吗?基本上,我想要一个允许每个键具有多个值的东西。我希望能够做类似的事情

dict.Add(key, val);

如果该键尚不存在,它将对其进行添加;如果已存在,它将仅对该键添加另一个值。我只是要遍历它,所以我真的不在乎其他检索方法。

回答:

它不存在,但是您可以从Dictionary and List快速构建一个:

class MultiDict<TKey, TValue>  // no (collection) base class

{

private Dictionary<TKey, List<TValue>> _data = new Dictionary<TKey,List<TValue>>();

public void Add(TKey k, TValue v)

{

// can be a optimized a little with TryGetValue, this is for clarity

if (_data.ContainsKey(k))

_data[k].Add(v)

else

_data.Add(k, new List<TValue>() { v}) ;

}

// more members

}

以上是 多值字典? 的全部内容, 来源链接: utcz.com/qa/407550.html

回到顶部