65.9K
CodeProject 正在变化。 阅读更多。
Home

在 VB.NET 和 C# 中迭代字典对象项目

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (1投票)

2013 年 10 月 11 日

CPOL
viewsIcon

40791

原则上,迭代字典对象可能看起来很复杂,但实际上却相当简单,这涉及到循环。

原则上,迭代字典对象可能看起来很复杂,但实际上却相当简单,这涉及到循环遍历字典中的每个 KeyValuePair 对象:-

VB.NET

Dim DictObj As New Dictionary(Of Integer, String)

DictObj.Add(1, "ABC")
DictObj.Add(2, "DEF")
DictObj.Add(3, "GHI")
DictObj.Add(4, "JKL")

For Each kvp As KeyValuePair(Of Integer, String) In DictObj
     Dim v1 As Integer = kvp.Key
     Dim v2 As String = kvp.Value
     Debug.WriteLine("Key: " + v1.ToString _
            + " Value: " + v2)
下一篇

C#

Dictionary<int, String> DictObj =
      new Dictionary<int, String>();

DictObj.Add(1, "ABC");
DictObj.Add(2, "DEF");
DictObj.Add(3, "GHI");
DictObj.Add(4, "JKL");

foreach (KeyValuePair<int,String> kvp in DictObj)
{
    int v1 = kvp.Key;
    String v2 = kvp.Value;
    Debug.WriteLine("Key: " + v1.ToString() +
         " Value: " + v2);
}

© . All rights reserved.