而不是使用Dictionary<TKey,TValue>
我想要某种类型的集合类,可以使用值的属性作为键,有类似的东西吗?
就在这里 -System.Collections.ObjectModel.KeyedCollection<TKey, TValue> http://msdn.microsoft.com/en-us/library/ms132438.aspx.
这是抽象的,据我所知,框架中没有具体的派生类,但您需要实现的只是GetKeyForItem
据我所知。例如,您可以使用委托来执行此操作:
public class DelegatingKeyedCollection<TKey, TItem> : System.Collections.ObjectModel.KeyedCollection<TKey, TItem>
{
private readonly Func<TItem, TKey> keySelector;
public DelegatingKeyedCollection(Func<TItem, TKey> keySelector)
{
this.keySelector = keySelector;
}
protected override TKey GetKeyForItem(TItem item)
{
return keySelector(item);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)