Để thêm khóa và giá trị vào StringDictionary, mã như sau -
Ví dụ
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);
}
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
StringDictionary elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad
Ví dụ
Hãy để chúng tôi xem một ví dụ khác -
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("1", "Electric Cars");
strDict.Add("2", "SUV");
strDict.Add("3", "AUV");
Console.WriteLine("StringDictionary elements...");
foreach(DictionaryEntry de in strDict) {
Console.WriteLine(de.Key + " " + de.Value);
}
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
StringDictionary elements... 1 Electric Cars 2 SUV 3 AUV