Jer*_*emy 7 .net collections dictionary
而不是使用Dictionary<TKey,TValue>我想要某种类型的集合类,可以使用值的属性作为键,是否有这样的东西?
Jon*_*eet 10
是的,有 - System.Collections.ObjectModel.KeyedCollection<TKey, TValue>.
这是抽象的,据我所知,框架中没有具体的派生类,但是你需要实现的就是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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
226 次 |
| 最近记录: |