在C#中如何创建一个StringDictionary?
要在C#中创建StringDictionary,代码如下-
示例
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("A", "John");
strDict.Add("B", "Andy");
strDict.Add("C", "Tim");
strDict.Add("D", "Ryan");
strDict.Add("E", "Kevin");
strDict.Add("F", "Katie");
strDict.Add("G", "Brad");
Console.WriteLine("StringDictionary elements...");
foreach(DictionaryEntry de in strDict) {
Console.WriteLine(de.Key + " " + de.Value);
}
}
}输出结果
这将产生以下输出-
StringDictionary elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad
示例
让我们看另一个例子-
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary myDict = new StringDictionary();
myDict.Add("1", "Tablet");
myDict.Add("2", "Desktop");
myDict.Add("3", "Speakers");
myDict.Add("4", "Laptop");
myDict.Add("5", "Notebook");
myDict.Add("6", "Ultrabook");
myDict.Add("7", "HDD");
myDict.Add("8", "SDD");
myDict.Add("9", "Headphone");
myDict.Add("10", "Earphone");
Console.WriteLine("Value for key 5 = "+myDict["5"]);
}
}输出结果
这将产生以下输出-
Value for key 5 = Notebook
热门推荐
10 生日祝福语 男性简短独特
11 康复走路祝福语大全简短
12 送教授贺卡祝福语简短
13 婚礼伴娘祝福语大全简短
14 庆祝当生日祝福语简短
15 平安男朋友祝福语简短
16 走正路的祝福语简短
17 新郎致辞诗句祝福语简短
18 生日奥特曼可爱祝福语简短