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

เมธอด Dictionary.Remove ใน C# พร้อมตัวอย่าง


บทนำ

เมธอด Dictionary.Remove() ใน C# ใช้ลบค่าที่มีคีย์ (key) ที่ระบุออกจาก Dictionary<TKey, TValue> คืนค่า true หากลบสำเร็จ false หากไม่พบคีย์

ไวยากรณ์ (Syntax)

public bool Remove (TKey key);

โดยที่ key คือคีย์ที่ต้องการลบ

ตัวอย่างที่ 1: ลบคีย์และตรวจสอบจำนวนสมาชิก

using System;
using System.Collections.Generic;

public class Demo 
{
    public static void Main()
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();
        dict.Add("One", "Kagido");
        dict.Add("Two", "Ngidi");
        dict.Add("Three", "Devillers");
        dict.Add("Four", "Smith");
        dict.Add("Five", "Warner");
        
        Console.WriteLine("Count of elements = " + dict.Count);
        Console.WriteLine("Removing some keys...");
        
        dict.Remove("Four");
        dict.Remove("Five");
        
        Console.WriteLine("Count of elements (updated) = " + dict.Count);
        Console.WriteLine("\nKey/value pairs...");
        
        foreach (KeyValuePair<string, string> res in dict)
        {
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
        }
        
        Console.Write("\nAll the keys..\n");
        Dictionary<string, string>.KeyCollection allKeys = dict.Keys;
        foreach (string str in allKeys)
        {
            Console.WriteLine("Key = {0}", str);
        }
    }
}

ผลลัพธ์

Count of elements = 5
Removing some keys...
Count of elements (updated) = 3
Key/value pairs...
Key = One, Value = Kagido
Key = Two, Value = Ngidi
Key = Three, Value = Devillers
All the keys..
Key = One
Key = Two
Key = Three

ตัวอย่างที่ 2: ลบคีย์และแสดงสถานะก่อน/หลัง

using System;
using System.Collections.Generic;

public class Demo 
{
    public static void Main()
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();
        dict.Add("One", "Kagido");
        dict.Add("Two", "Ngidi");
        dict.Add("Three", "Devillers");
        dict.Add("Four", "Smith");
        dict.Add("Five", "Warner");
        
        Console.WriteLine("Count of elements = " + dict.Count);
        Console.Write("\nAll the keys..\n");
        
        Dictionary<string, string>.KeyCollection allKeys = dict.Keys;
        foreach (string str in allKeys)
        {
            Console.WriteLine("Key = {0}", str);
        }
        
        Console.WriteLine("Removing some keys...");
        dict.Remove("Four");
        dict.Remove("Five");
        
        Console.WriteLine("Count of elements (updated) = " + dict.Count);
        Console.Write("\nAll the keys..updated\n");
        foreach (string str in allKeys)
        {
            Console.WriteLine("Key = {0}", str);
        }
        
        Console.WriteLine("\nKey/value pairs...");
        foreach (KeyValuePair<string, string> res in dict)
        {
            Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value);
        }
    }
}

ผลลัพธ์

Count of elements = 5
All the keys..
Key = One
Key = Two
Key = Three
Key = Four
Key = Five
Removing some keys...
Count of elements (updated) = 3
All the keys..updated
Key = One
Key = Two
Key = Three
Key/value pairs...
Key = One, Value = Kagido
Key = Two, Value = Ngidi
Key = Three, Value = Devillers

สรุป

เมธอด Remove() เป็นวิธีมาตรฐานในการลบรายการจาก Dictionary ตามคีย์ หากต้องการลบหลายรายการพร้อมกัน สามารถใช้ LINQ: keysToRemove.ForEach(k => dict.Remove(k)) หรือสร้าง Dictionary ใหม่ด้วย Where คีย์ที่ไม่มีอยู่จริงจะคืน false แต่ไม่โยนข้อยกเว้น