Dictionary的遍历

参考:https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.keyvaluepair-2?view=netframework-4.7

KeyValuePair<TKey,TValue> 结构

定义可以设置或检索的键/值对。

[System.Serializable]
public struct KeyValuePair<TKey,TValue>
Dictionary<string, string> openWith =
    new Dictionary<string, string>();

openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
{
    Console.WriteLine("Key = {0}, Value = {1}",
        kvp.Key, kvp.Value);
}
foreach( KeyValuePair<string, string> kvp in myDictionary )
{
    Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
构造函数
KeyValuePair<TKey,TValue>(TKey, TValue)

用指定的键和值初始化 KeyValuePair<TKey,TValue> 结构的新实例。

 

属性
Key

获取键/值对中的键。

Value

获取键/值对中的值。

 

方法
ToString()

使用键和值的字符串表示形式返回 KeyValuePair<TKey,TValue> 的字符串表示形式。

 

原文地址:https://www.cnblogs.com/strive-19970713/p/13796670.html