ในการรับหรือตั้งค่าที่เกี่ยวข้องกับคีย์ที่ระบุใน SortedList รหัสจะเป็นดังนี้ -
ตัวอย่าง
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"]);
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Value associated with key E = Clothing Value associated with key E [Updated] = HDD
ตัวอย่าง
เรามาดูตัวอย่างกัน −
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"]);
}
} ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
Value associated with key D = Pet Supplies