[英]Generic form of NameValueCollection in .Net
Does .Net provides generic form of NameValueCollection or an alternative to Dictionary<string,List<T>>
? .Net 是否提供通用形式的 NameValueCollection 或
Dictionary<string,List<T>>
的替代品?
Something like就像是
Person john = new Person();
...
Person vick = new Person();
...
NameValueCollection<Person> stringToPerson = new NameValueCollection<Person>();
stringToPerson.Add("John",john)
stringToPerson.Add("Vick",vick)
Actually in my case am forced to rely on Dictionary<string,List<Peron>>
, is there any other alternative?实际上在我的情况下被迫依赖
Dictionary<string,List<Peron>>
,还有其他选择吗?
Regards, Jeez问候, 吉兹
The closest alternative is probably the ILookup<TKey, TElement>
interface. 最接近的替代方案可能是
ILookup<TKey, TElement>
接口。 At the moment, the only public type that implements it in the BCL is the immutable Lookup<TKey, TElement>
class, an instance of which can be created with the Enumerable.ToLookup
method. 目前,在BCL中实现它的唯一公共类型是不可变的
Lookup<TKey, TElement>
类,其实例可以使用Enumerable.ToLookup
方法创建。 If you want a mutable type that implements the interface, you'll have to write one yourself; 如果你想要一个实现接口的可变类型,你必须自己写一个; you can find an example implementation here .
你可以在这里找到一个示例实现。
In your case, you probably want an ILookup<string, Person>
. 在你的情况下,你可能想要一个
ILookup<string, Person>
。
There's no such thing built in to the BCL as far as I know. 据我所知,BCL没有内置这样的东西。 I would just write your own class which wraps a
Dictionary<string, List<T>>
internally and exposes appropriate methods (eg, Add
could add an element to the List<T>
for the given key). 我只想编写你自己的类,它在内部包含
Dictionary<string, List<T>>
并公开适当的方法(例如, Add
可以为给定键的List<T>
添加一个元素)。
For example: 例如:
class NameValueCollection<T>
{
Dictionary<string, List<T>> _dict = new Dictionary<string, List<T>>();
public void Add(string name, T value)
{
List<T> list;
if (!_dict.TryGetValue(name, out list))
{
_dict[name] = list = new List<T>();
}
list.Add(value);
}
// etc.
}
Oh, I see what you want to do now. 哦,我明白你现在想做什么。 You want to be able to add to the Person collection without having to create a new List each time.
您希望能够添加到Person集合,而无需每次都创建新的List。 Extension methods to the rescue!
救援的延伸方法!
public static void SafeAdd<TValue>(this IDictionary<TKey, ICollection<TValue>> dict, TKey key, TValue value)
{
HashSet<T> container;
if (!dict.TryGetValue(key, out container))
{
dict[key] = new HashSet<TValue>();
}
dict[key].Add(value);
}
Usage:
var names = new Dictionary<string, ICollection<Person>>();
names.SafeAdd("John", new Person("John"));
Here is a C# example of complete implementation of the generic NameValueCollection based on NameObjectCollectionBase .这是基于NameObjectCollectionBase的通用NameValueCollection的完整实现的 C# 示例。 What makes this collection special unlike Dictionary , is that one key can contain several elements.
与Dictionary不同的是,这个集合的特别之处在于一个键可以包含多个元素。
Usage:用法:
NameValueCollection<int> collection = new NameValueCollection<int>();
collection.Add("a", 123);
collection.Add("a", 456); // 123 and 456 will be inserted into the same key.
collection.Add("b", 789); // 789 will be inserted into another key.
int[] a = collection.Get("a"); // contains 132 and 456.
int[] b = collection.Get("b"); // contains 789.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.