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