C# 如何遍历 SortedList,同时获取键和值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14013261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to loop through a SortedList, getting both the key and the value
提问by Wayneio
The following code loops through a list and gets the values, but how would I write a similar statement that gets both the keys and the values
下面的代码循环遍历一个列表并获取值,但是我将如何编写一个类似的语句来获取键和值
foreach (string value in list.Values)
{
Console.WriteLine(value);
}
e.g something like this
例如这样的事情
foreach (string value in list.Values)
{
Console.WriteLine(value);
Console.WriteLine(list.key);
}
code for the list is:
该列表的代码是:
SortedList<string, string> list = new SortedList<string, string>();
采纳答案by Sergey Berezovskiy
foreach (KeyValuePair<string, string> kvp in list)
{
Console.WriteLine(kvp.Value);
Console.WriteLine(kvp.Key);
}
From msdn:
从msdn:
GetEnumerator returns an enumerator of type KeyValuePair<TKey, TValue>
that iterates through the SortedList<TKey, TValue>
.
GetEnumerator 返回一个类型的枚举器,该枚举器KeyValuePair<TKey, TValue>
遍历SortedList<TKey, TValue>
.
As Jon stated, you can use var
keyword instead of writing type name of iteration variable (type will be inferred from usage):
正如 Jon 所说,您可以使用var
关键字而不是编写迭代变量的类型名称(类型将根据使用情况推断):
foreach (var kvp in list)
{
Console.WriteLine(kvp.Value);
Console.WriteLine(kvp.Key);
}
回答by Chu Chau
bool value = list[key];
this can help you.
这可以帮助你。
回答by gotqn
Just iterated using the keys and get the value for each key:
只需使用键进行迭代并获取每个键的值:
SortedList<string, string> info = new SortedList<string, string>();
info.Add("path", "път");
info.Add("folder", "директория");
info.Add("directory", "директория");
info.Add("file", "Файл");
foreach (string key in info.Keys)
{
Console.WriteLine("\t{0}\t{1}", key, info[key]);
}
回答by Dave
Apologies...
道歉...
this was wrong topic (loop through values). My issue was looping through the key value pairs, not just the values. Will leave this here if there are no objections as a possible option to get values from SortedList collection.
这是错误的主题(循环遍历值)。我的问题是遍历键值对,而不仅仅是值。如果没有反对意见,将把它留在这里作为从 SortedList 集合中获取值的可能选项。
I just tried to figure out this error as well and my solution was to use the DictionaryEntry
type to replace the erroring out KeyValuePair
type.
Found from MS reference
https://msdn.microsoft.com/en-us/library/system.collections.dictionaryentry(v=vs.100).aspx
我也只是试图找出这个错误,我的解决方案是使用该DictionaryEntry
类型来替换出错的KeyValuePair
类型。从 MS 参考https://msdn.microsoft.com/en-us/library/system.collections.dictionaryentry(v=vs.100).aspx 中找到
In my case, I had code creating a SortedList
type collection and neither the var
or KeyValuePair
types worked (var
errored out when trying to read the key/value from the collection item and the KeyValuePair
errored out in the initial loop definition. Both errored out with "Specified cast is not valid"
)
就我而言,我有代码创建了一个SortedList
类型集合,但var
orKeyValuePair
类型都不起作用(var
尝试从集合项中读取键/值时KeyValuePair
出错,并且在初始循环定义中出错。两者都出错了"Specified cast is not valid"
)
So here is sample of code that worked for me:
所以这里是对我有用的代码示例:
SortedList _dims = GetList("mysortedlist");
foreach (DictionaryEntry kvp in _dims)
{
Console.WriteLine(kvp.Key.ToString());
Console.WriteLine(kvp.Value.ToString());
}
HTH
HTH
Dave
戴夫