获取一个遍历C#中Dictionary的枚举数
为了获得遍历字典的枚举数,代码如下-
示例
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(100, "Laptop");
dict.Add(150, "Desktop");
dict.Add(200, "Earphone");
dict.Add(300, "Tablet");
dict.Add(500, "Speakers");
dict.Add(750, "HardDisk");
dict.Add(1000, "SSD");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
Console.WriteLine("Enumerator iterating key-value pairs...");
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}输出结果
这将产生以下输出-
Enumerator iterating key-value pairs... Key = 100, Value = Laptop Key = 150, Value = Desktop Key = 200, Value = Earphone Key = 300, Value = Tablet Key = 500, Value = Speakers Key = 750, Value = HardDisk Key = 1000, Value = SSD
示例
让我们看另一个例子-
using System;
using System.Collections;
using System.Collections.Generic;
public class Demo {
public static void Main(){
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("One", "Laptop");
dict.Add("Two", "Desktop");
dict.Add("Three", "Earphone");
dict.Add("Four", "Tablet");
dict.Add("Five", "Speakers");
dict.Add("Six", "HardDisk");
dict.Add("Seven", "SSD");
dict.Add("Eight", "Keyboard");
dict.Add("Nine", "Mouse");
IDictionaryEnumerator demoEnum = dict.GetEnumerator();
Console.WriteLine("Enumerator iterating key-value pairs...");
while (demoEnum.MoveNext())
Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
}
}输出结果
这将产生以下输出-
Enumerator iterating key-value pairs... Key = One, Value = Laptop Key = Two, Value = Desktop Key = Three, Value = Earphone Key = Four, Value = Tablet Key = Five, Value = Speakers Key = Six, Value = HardDisk Key = Seven, Value = SSD Key = Eight, Value = Keyboard Key = Nine, Value = Mouse
热门推荐
7 薛之谦祝福语简短
10 新年祝福语长辈简短红包
11 水晶婚夫妻祝福语简短
12 18岁的祝福语简短
13 恭喜订婚的祝福语简短
14 结婚祝福语亲姐姐简短
15 小清新的祝福语简短
16 前程祝福语简短暖心
17 婚礼美句简短祝福语
18 妈妈生日贺卡祝福语 简短