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

📅  最后修改于: 2021-05-29 17:06:46             🧑  作者: Mango

StringDictionary.Remove(String)方法用于从字符串字典中删除具有指定键的条目

句法:

public virtual void Remove (string key);

在此, key是要删除的条目的键。

例外情况:

  • ArgumentNullException:如果键为null。
  • NotSupportedException:如果StringDictionary是只读的。

下面的程序说明了StringDictionary.Remove(String)方法的用法:

范例1:

// C# code to remove the entry
// with the specified key from
// the StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named myDict
        StringDictionary myDict = new StringDictionary();
  
        // Adding key and value into the StringDictionary
        myDict.Add("A", "Apple");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
        myDict.Add("E", "Elephant");
        myDict.Add("F", "Fish");
  
        // Displaying the keys and values in StringDictionary
        Console.WriteLine("The number of key/value pairs are : " + myDict.Count);
  
        foreach(DictionaryEntry dic in myDict)
        {
            Console.WriteLine(dic.Key + " " + dic.Value);
        }
  
        // Removing the entry with the specified
        // key from the StringDictionary
        myDict.Remove("D");
  
        // Displaying the keys and values in StringDictionary
        Console.WriteLine("The number of key/value pairs are : " + myDict.Count);
  
        foreach(DictionaryEntry dic in myDict)
        {
            Console.WriteLine(dic.Key + " " + dic.Value);
        }
    }
}

输出:

The number of key/value pairs are : 6
b Banana
c Cat
a Apple
f Fish
d Dog
e Elephant
The number of key/value pairs are : 5
b Banana
c Cat
a Apple
f Fish
e Elephant

范例2:

// C# code to remove the entry
// with the specified key from
// the StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named myDict
        StringDictionary myDict = new StringDictionary();
  
        // Adding key and value into the StringDictionary
        myDict.Add("A", "Apple");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
        myDict.Add("E", "Elephant");
        myDict.Add("F", "Fish");
  
        // Displaying the keys and values in StringDictionary
        Console.WriteLine("The number of key/value pairs are : " + myDict.Count);
  
        foreach(DictionaryEntry dic in myDict)
        {
            Console.WriteLine(dic.Key + " " + dic.Value);
        }
  
        // Removing the entry with the specified
        // key from the StringDictionary
        // This should raise "ArgumentNullException"
        // as the key is null
        myDict.Remove(null);
  
        // Displaying the keys and values in StringDictionary
        Console.WriteLine("The number of key/value pairs are : " + myDict.Count);
  
        foreach(DictionaryEntry dic in myDict)
        {
            Console.WriteLine(dic.Key + " " + dic.Value);
        }
    }
}

运行时错误:

笔记:

  • 如果StringDictionary不包含带有指定键的元素,则StringDictionary保持不变。没有异常被抛出。
  • 密钥以不区分大小写的方式处理,即,在用于查找要从StringDictionary中删除的条目之前将其转换为小写。
  • 此方法是O(1)操作。

参考:

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