C# |从字符串中删除带有指定键的条目

原文:https://www . geesforgeks . org/c-sharp-remove-entry-with-specified-key-from-stringdictionary/

StringDictionary。移除(字符串)方法用于从字符串字典中移除带有指定键的条目。

语法:

public virtual void Remove (string key);

在这里, 是要移除的条目的键。

异常:

  • ArgumentNullException: 如果密钥为空。
  • notSupportDexception:如果 StringDictionary 是只读的。

下面的程序说明了字符串的使用。移除(字符串)方法:

例 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);
        }
    }
}

运行时错误:

未处理异常: 系统。ArgumentNullException:值不能为空。 参数名称:键

注:

  • 如果 StringDictionary 不包含具有指定键的元素,则 StringDictionary 保持不变。不会引发异常。
  • 该键以不区分大小写的方式进行处理,即在用于从 StringDictionary 中查找要移除的条目之前,将其转换为小写。
  • 这个方法是一个 O(1)运算。

参考: