gpt4 book ai didi

c# - 可通过索引和键访问的内置列表

转载 作者:太空狗 更新时间:2023-10-30 00:22:39 25 4
gpt4 key购买 nike

是否可以创建一个可以通过索引或键访问的列表?

我正在寻找一个已经存在但具有此功能的集合类型,我想避免重新定义索引器

最佳答案

What is the best data structure in .NET for look-up by string key or numeric index? 也有类似的问题.

看看KeyedCollection :

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);
}
}

关于c# - 可通过索引和键访问的内置列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/298281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com