Để lấy hoặc đặt giá trị được liên kết với khóa được chỉ định trong StringDictionary, mã như sau -
Ví dụ
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary ();
strDict.Add("A", "Books");
strDict.Add("B", "Electronics");
strDict.Add("C", "Appliances");
strDict.Add("D", "Pet Supplies");
strDict.Add("E", "Clothing");
strDict.Add("F", "Footwear");
Console.WriteLine("Value associated with key D = "+strDict["D"]);
Console.WriteLine("Value associated with key F = "+strDict["F"]);
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
Value associated with key D = Pet Supplies Value associated with key F = Footwear
Ví dụ
Hãy để chúng tôi xem một ví dụ khác -
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary ();
strDict.Add("A", "Books");
strDict.Add("B", "Electronics");
strDict.Add("C", "Appliances");
strDict.Add("D", "Pet Supplies");
strDict.Add("E", "Clothing");
strDict.Add("F", "Footwear");
Console.WriteLine("Value associated with key D = "+strDict["D"]);
Console.WriteLine("Value associated with key F = "+strDict["F"]);
strDict["F"] = "HDD";
Console.WriteLine("Value associated with key F (UPDATED) = "+strDict["F"]);
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
Value associated with key D = Pet Supplies Value associated with key F = Footwear Value associated with key F (UPDATED) = HDD