📌  相关文章
📜  C#|从ListDictionary中删除具有指定键的条目

📅  最后修改于: 2021-05-29 20:22:21             🧑  作者: Mango

ListDictionary.Remove(Object)方法用于从ListDictionary中删除具有指定键的条目。

句法:

public void Remove (object key);

此处,是要删除的条目的键。

异常:如果键为null,则此方法将提供ArgumentNullException。

以下示例说明了ListDictionary.Remove(Object)方法的用法:

范例1:

// C# code to remove the entry with
// the specified key from the ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a ListDictionary named myDict
        ListDictionary myDict = new ListDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("Australia", "Canberra");
        myDict.Add("Belgium", "Brussels");
        myDict.Add("Netherlands", "Amsterdam");
        myDict.Add("China", "Beijing");
        myDict.Add("Russia", "Moscow");
        myDict.Add("India", "New Delhi");
  
        // To get count of key/value pairs in myDict
        Console.WriteLine("Total key/value pairs in myDict are : " 
                                                  + myDict.Count);
  
        // Displaying the key/value pairs in myDict
        Console.WriteLine("The key/value pairs in myDict are : ");
  
        foreach(DictionaryEntry de in myDict)
        {
            Console.WriteLine(de.Key + " " + de.Value);
        }
  
        // Remove the entry with the specified
        // key from the ListDictionary
        myDict.Remove("Russia");
  
        // To get count of key/value pairs in myDict
        Console.WriteLine("Total key/value pairs in myDict are : " 
                                                 + myDict.Count);
  
        // Displaying the key/value pairs in myDict
        Console.WriteLine("The key/value pairs in myDict are : ");
  
        foreach(DictionaryEntry de in myDict)
        {
            Console.WriteLine(de.Key + " " + de.Value);
        }
    }
}

输出:

Total key/value pairs in myDict are : 6
The key/value pairs in myDict are : 
Australia Canberra
Belgium Brussels
Netherlands Amsterdam
China Beijing
Russia Moscow
India New Delhi
Total key/value pairs in myDict are : 5
The key/value pairs in myDict are : 
Australia Canberra
Belgium Brussels
Netherlands Amsterdam
China Beijing
India New Delhi

范例2:

// C# code to remove the entry with
// the specified key from the ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a ListDictionary named myDict
        ListDictionary myDict = new ListDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("I", "first");
        myDict.Add("II", "second");
        myDict.Add("III", "third");
        myDict.Add("IV", "fourth");
        myDict.Add("V", "fifth");
  
        // To get count of key/value pairs in myDict
        Console.WriteLine("Total key/value pairs in myDict are : " 
                                                  + myDict.Count);
        // Displaying the key/value pairs in myDict
  
        Console.WriteLine("The key/value pairs in myDict are : ");
  
        foreach(DictionaryEntry de in myDict)
        {
            Console.WriteLine(de.Key + " " + de.Value);
        }
  
        // Remove the entry with the specified
        // key from the ListDictionary
        // This should raise "ArgumentNullException"
        // as the key is null
        myDict.Remove(null);
  
        // To get count of key/value pairs in myDict
        Console.WriteLine("Total key/value pairs in myDict are : " 
                                                  + myDict.Count);
  
        // Displaying the key/value pairs in myDict
  
        Console.WriteLine("The key/value pairs in myDict are : ");
  
        foreach(DictionaryEntry de in myDict)
        {
            Console.WriteLine(de.Key + " " + de.Value);
        }
    }
}

运行时错误:

笔记:

  • 如果ListDictionary不包含带有指定键的元素,则ListDictionary保持不变。没有异常被抛出。
  • 此方法是O(n)运算,其中n是Count。

参考 :

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.listdictionary.remove?view=netframework-4.7.2