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

เมธอด SortedDictionary.Clear() ใน C# : ล้างข้อมูลทั้งหมด

เมธอด SortedDictionary<TKey, TValue>.Clear() ใน C# ใช้ลบสมาชิกทั้งหมดออกจาก SortedDictionary ทำให้ Count = 0 แต่ความจุภายในอาจยังคงอยู่ (ไม่ปล่อยหน่วยความจำทันที)

รูปแบบไวยากรณ์ (Syntax)

public void Clear();

ตัวอย่างที่ 1: Clear พื้นฐานพร้อม ContainsKey

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        SortedDictionary sortedDict = new SortedDictionary();
        
        sortedDict.Add(100, "Mobile");
        sortedDict.Add(200, "Laptop");
        sortedDict.Add(300, "Desktop");
        sortedDict.Add(400, "Speakers");
        sortedDict.Add(500, "Headphone");
        sortedDict.Add(600, "Earphone");
        
        Console.WriteLine("SortedDictionary key-value pairs...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
        
        Console.WriteLine("Count = " + sortedDict.Count);
        Console.WriteLine("\nContains Key 200? = " + sortedDict.ContainsKey(200));
        
        sortedDict.Clear();
        
        Console.WriteLine("Count after Clear = " + sortedDict.Count);
    }
}

ผลลัพธ์

SortedDictionary key-value pairs...
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 400, Value = Speakers
Key = 500, Value = Headphone
Key = 600, Value = Earphone
Count of SortedDictionary key-value pairs = 6
The SortedDictionary has the key 200? = True
Count of SortedDictionary key-value pairs = 0

ตัวอย่างที่ 2: Clear หลังจากเพิ่มข้อมูลใหม่

using System;
using System.Collections;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        SortedDictionary sortedDict = new SortedDictionary();
        
        sortedDict.Add(100, "Inspiron");
        sortedDict.Add(200, "Alienware");
        sortedDict.Add(300, "Projectors");
        sortedDict.Add(400, "XPS");
        
        Console.WriteLine("SortedDictionary key-value pairs...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
        
        Console.WriteLine("Count = " + sortedDict.Count);
        
        sortedDict.Add(800, "Notebook");
        sortedDict.Add(10000, "Bluetooth Speaker");
        
        Console.WriteLine("\nSortedDictionary key-value pairs...UPDATED");
        demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }
        
        Console.WriteLine("Count (UPDATED) = " + sortedDict.Count);
        
        sortedDict.Clear();
        
        Console.WriteLine("\nCount after Clear = " + sortedDict.Count);
    }
}

ผลลัพธ์

SortedDictionary key-value pairs...
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
Count = 4
SortedDictionary key-value pairs...UPDATED
Key = 100, Value = Inspiron
Key = 200, Value = Alienware
Key = 300, Value = Projectors
Key = 400, Value = XPS
Key = 800, Value = Notebook
Key = 10000, Value = Bluetooth Speaker
Count (UPDATED) = 6
Count after Clear = 0

สรุปและข้อสังเกต

  • Clear() ลบ ทุก key-value pairs ทำให้ Count = 0
  • ไม่คืนค่า (void) และไม่โยน exception แม้ Dictionary จะว่างอยู่แล้ว
  • ความจุภายใน (internal capacity) อาจไม่ลดลง - เพื่อประสิทธิภาพการ Add ต่อไป
  • หากต้องการปล่อยหน่วยความจำจริง ให้สร้าง instance ใหม่: dict = new SortedDictionary<TKey, TValue>();
  • Complexity: O(n) โดย n = จำนวนสมาชิก (ต้อง clear ทุก node ใน tree)
  • หลัง Clear() ใช้ ContainsKey() จะได้ false เสมอ

เปรียบเทียบ Clear() กับ การสร้างใหม่

// วิธี 1: Clear (เก็บ capacity ไว้ใช้ต่อ)
dict.Clear();
dict.Add(...); // เร็วขึ้นหากเพิ่มข้อมูลใหม่ทันที

// วิธี 2: ใหม่ทั้งอัน (ปล่อย memory)
dict = new SortedDictionary(); // GC จะเก็บอันเก่า