Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Values Property ของ SortedList Class ใน C#

SortedList ใน C# เป็นคอลเลกชันที่เก็บ Key-Value Pairs เรียงลำดับตาม Key โดยอัตโนมัติ Property Values ใช้ดึงค่า (Values) ทั้งหมดใน SortedList ออกมาเป็น ICollection (เรียงตาม Key)

การประกาศและเพิ่มข้อมูล

using System.Collections;

SortedList list = new SortedList();
list.Add("S1", "Wallets");
list.Add("S2", "Sunglasses");
list.Add("S3", "Backpacks");
// เรียงตาม Key: S1, S2, S3

ใช้งาน Values Property

using System;
using System.Collections;

namespace Demo {
    class Program {
        static void Main(string[] args) {
            SortedList list = new SortedList();
            list.Add("S1", "Wallets");
            list.Add("S2", "Sunglasses");
            list.Add("S3", "Backpacks");
            
            // Values Property - คืน ICollection ของ Values (เรียงตาม Key)
            foreach (string value in list.Values) {
                Console.WriteLine(value);
            }
        }
    }
}

ผลลัพธ์

Wallets
Sunglasses
Backpacks

Properties และ Methods สำคัญของ SortedList

Member คำอธิบาย
Keys ICollection ของ Keys (เรียงลำดับ)
Values ICollection ของ Values (เรียงตาม Key)
Count จำนวน Key-Value Pairs
IsFixedSize มีขนาดคงที่หรือไม่ (Always False)
IsReadOnly Read-only หรือไม่ (Always False)
Item[key] Indexer - Get/Set ค่าด้วย Key
Add(key, value) เพิ่ม Key-Value Pair
Remove(key) ลบด้วย Key
RemoveAt(index) ลบด้วย Index (0-based, ตามลำดับ Key)
ContainsKey(key) ตรวจสอบ Key
ContainsValue(value) ตรวจสอบ Value
GetByIndex(index) ดึง Value ด้วย Index
GetKey(index) ดึง Key ด้วย Index
IndexOfKey(key) หา Index ของ Key
IndexOfValue(value) หา Index แรกของ Value

Generic Version: SortedList<TKey, TValue> (แนะนำ)

using System.Collections.Generic;

var sortedList = new SortedList {
    { "S1", "Wallets" },
    { "S2", "Sunglasses" },
    { "S3", "Backpacks" }
};

// Type-safe Values
ICollection values = sortedList.Values;
foreach (string v in values) Console.WriteLine(v);

// Keys Property
foreach (string k in sortedList.Keys) Console.WriteLine(k);

SortedList vs SortedDictionary vs Dictionary

Feature SortedList<TKey,TValue> SortedDictionary<TKey,TValue> Dictionary<TKey,TValue>
Ordering Sorted by Key Sorted by Key No Order (Hash-based)
Insert/Delete O(n) - Array shift O(log n) - Tree O(1) Average
Memory Less (Arrays) More (Nodes) Medium
Index Access ✅ By Index (Values[0]) ❌ No Index ❌ No Index
ใช้เมื่อ Small data, Frequent Index Access Large data, Frequent Insert/Delete General Purpose, Fast Lookup

ตัวอย่าง: Iterate Keys และ Values พร้อมกัน

var sortedList = new SortedList {
    { 3, "Three" },
    { 1, "One" },
    { 2, "Two" }
};

// Index-based (SortedList เท่านั้น)
for (int i = 0; i < sortedList.Count; i++) {
    Console.WriteLine($"Index {i}: Key={sortedList.Keys[i]}, Value={sortedList.Values[i]}");
}

/* Output:
Index 0: Key=1, Value=One
Index 1: Key=2, Value=Two
Index 2: Key=3, Value=Three */

Performance Note

  • Values Property คืนค่า **View** (ไม่ Copy) - O(1)
  • Iteration เร็ว เพราะเก็บใน Array ภายใน
  • Insert/Delete ช้า O(n) - ต้อง Shift Array