Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Phương thức SortedDictionary.Remove() trong C#

Phương thức SortedDictionary<TKey, TValue>.Remove() trong C# được dùng để xóa phần tử có khóa (key) được chỉ định khỏi SortedDictionary.

Cú pháp

public bool Remove(TKey key);
  • key: Khóa của phần tử cần xóa.

Trả về: true nếu phần tử được tìm thấy và xóa thành công; false nếu khóa không tồn tại.

Ví dụ 1: Xóa key 400 từ SortedDictionary

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("Các cặp key-value trong SortedDictionary...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }

        Console.WriteLine("\nXóa key 400: " + sortedDict.Remove(400));

        Console.WriteLine("\nCác cặp key-value SAU KHI CẬP NHẬT...");
        demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }

        SortedDictionary.KeyCollection keyColl = sortedDict.Keys;
        Console.WriteLine("\nDanh sách Keys...");
        foreach (int i in keyColl)
        {
            Console.WriteLine(i);
        }
    }
}

Kết quả

Các cặp key-value trong SortedDictionary...
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 400, Value = Speakers
Key = 500, Value = Headphone
Key = 600, Value = Earphone

Xóa key 400: True

Các cặp key-value SAU KHI CẬP NHẬT...
Key = 100, Value = Mobile
Key = 200, Value = Laptop
Key = 300, Value = Desktop
Key = 500, Value = Headphone
Key = 600, Value = Earphone

Danh sách Keys...
100
200
300
500
600

Ví dụ 2: Xóa key 7

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

public class Demo
{
    public static void Main()
    {
        SortedDictionary sortedDict = new SortedDictionary();

        sortedDict.Add(1, "Ultrabook");
        sortedDict.Add(2, "Alienware");
        sortedDict.Add(3, "Notebook");
        sortedDict.Add(4, "Connector");
        sortedDict.Add(5, "Flash Drive");
        sortedDict.Add(6, "SSD");
        sortedDict.Add(7, "HDD");
        sortedDict.Add(8, "Earphone");

        Console.WriteLine("Các cặp key-value...");
        IDictionaryEnumerator demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }

        Console.WriteLine("\nXóa key 7: " + sortedDict.Remove(7));

        Console.WriteLine("\nCác cặp key-value SAU KHI CẬP NHẬT...");
        demoEnum = sortedDict.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            Console.WriteLine("Key = " + demoEnum.Key + ", Value = " + demoEnum.Value);
        }

        SortedDictionary.KeyCollection keyColl = sortedDict.Keys;
        Console.WriteLine("\nDanh sách Keys...");
        foreach (int i in keyColl)
        {
            Console.WriteLine(i);
        }
    }
}

Kết quả

Các cặp key-value...
Key = 1, Value = Ultrabook
Key = 2, Value = Alienware
Key = 3, Value = Notebook
Key = 4, Value = Connector
Key = 5, Value = Flash Drive
Key = 6, Value = SSD
Key = 7, Value = HDD
Key = 8, Value = Earphone

Xóa key 7: True

Các cặp key-value SAU KHI CẬP NHẬT...
Key = 1, Value = Ultrabook
Key = 2, Value = Alienware
Key = 3, Value = Notebook
Key = 4, Value = Connector
Key = 5, Value = Flash Drive
Key = 6, Value = SSD
Key = 8, Value = Earphone

Danh sách Keys...
1
2
3
4
5
6
8

Nạp chồng khác: Remove(TKey, out TValue)

public bool Remove(TKey key, out TValue value);

Xóa và trả về giá trị bị xóa qua tham số out (hữu ích khi cần giá trị trước khi xóa).

Ví dụ

if (sortedDict.Remove(400, out string removedValue))
{
    Console.WriteLine("Đã xóa: " + removedValue); // Speakers
}

Lưu ý

  • SortedDictionary duy trì key theo thứ tự tăng dần (sử dụng cây đỏ-đen Red-Black tree nội bộ).
  • Phức độ xóa: O(log n).
  • Không ném ngoại lệ nếu key không tồn tại (trả về false).
  • Ném ArgumentNullException nếu key là null (với reference type key).