是否可以创建一个可以通过索引或键访问的列表?
我正在寻找一个已经存在但具有此功能的集合类型,我想避免重新定义索引器
有一个类似的问题.NET 中通过字符串键或数字索引查找的最佳数据结构是什么? https://stackoverflow.com/questions/137753/what-is-the-best-data-structure-in-net-for-look-up-by-string-key-or-numeric-ind.
看一下键控集合 http://msdn.microsoft.com/en-us/library/ms132438.aspx:
class IndexableDictionary<TKey, TItem> : KeyedCollection<TKey, TItem>
{ Dictionary<TItem, TKey> keys = new Dictionary<TItem, TKey>();
protected override TKey GetKeyForItem(TItem item) { return keys[item];}
public void Add(TKey key, TItem item)
{ keys[item] = key;
this.Add(item);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)