gpt4 book ai didi

c# - 如何通过 OrderedDictionary 反向迭代

转载 作者:行者123 更新时间:2023-11-30 19:36:53 25 4
gpt4 key购买 nike

我如何遍历 OrderedDictionary反过来访问它的 key ?

由于它不支持 LINQ 扩展,我尝试了以下方法:

var orderedDictionary= new OrderedDictionary();
orderedDictionary.Add("something", someObject);
orderedDictionary.Add("another", anotherObject);

for (var dictIndex = orderedDictionary.Count - 1; dictIndex != 0; dictIndex--)
{
// It gives me the value, but how do I get the key?
// E.g., "something" and "another".
var key = orderedDictionary[dictIndex];
}

最佳答案

我可以建议使用 SortedDictionary<K, V> ?它确实支持 LINQ 并且类型安全:

var orderedDictionary = new SortedDictionary<string, string>();
orderedDictionary.Add("something", "a");
orderedDictionary.Add("another", "b");

foreach (KeyValuePair<string, string> kvp in orderedDictionary.Reverse())
{
}

此外,正如 Ivan Stoev 在评论中指出的那样,OrderedDictionary 的返回项根本没有订购,所以 SortedDictionary是你想要的。

关于c# - 如何通过 OrderedDictionary 反向迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41717399/

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