📌  相关文章
📜  C#|从OrderedDictionary中删除所有元素

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

OrderedDictionary.Clear方法用于从OrderedDictionary集合中删除所有元素。

句法:

public void Clear ();

异常:如果OrderedDictionary集合是只读的,则它将抛出NotSupportedException

下面给出了一些示例,以更好地理解实现:

范例1:

// C# code to remove all elements
// from OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver method
    public static void Main()
    {
  
        // Creating a orderedDictionary named myDict
        OrderedDictionary myDict = new OrderedDictionary();
  
        // Adding key and value in myDict
        myDict.Add("key1", "value1");
        myDict.Add("key2", "value2");
        myDict.Add("key3", "value3");
        myDict.Add("key4", "value4");
        myDict.Add("key5", "value5");
  
        // Displaying the number of element initially
        Console.WriteLine("Number of elements are : " + myDict.Count);
  
        // Displaying the elements in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " -- " + de.Value);
  
        // Removing all elements from OrderedDictionary
        myDict.Clear();
  
        // Displaying the number of element initially
        Console.WriteLine("Number of elements are : " + myDict.Count);
  
        // Displaying the elements in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " -- " + de.Value);
    }
}

输出:

Number of elements are : 5
key1 -- value1
key2 -- value2
key3 -- value3
key4 -- value4
key5 -- value5
Number of elements are : 0

范例2:

// C# code to remove all elements
// from OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver method
    public static void Main()
    {
  
        // Creating a orderedDictionary named myDict
        OrderedDictionary myDict = new OrderedDictionary();
  
        // Adding key and value in myDict
        myDict.Add("A", "Apple");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
  
        // Displaying the number of element initially
        Console.WriteLine("Number of elements are : " + myDict.Count);
  
        // Displaying the elements in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " -- " + de.Value);
  
        // Removing all elements from OrderedDictionary
        myDict.Clear();
  
        // Displaying the number of element initially
        Console.WriteLine("Number of elements are : " + myDict.Count);
  
        // Displaying the elements in myDict
        foreach(DictionaryEntry de in myDict)
            Console.WriteLine(de.Key + " -- " + de.Value);
    }
}

输出:

Number of elements are : 4
A -- Apple
B -- Banana
C -- Cat
D -- Dog
Number of elements are : 0

笔记:

  • 调用Clear方法后, Count属性设置为零。
  • 还释放了来自集合元素的其他对象的引用。
  • 调用此方法不会改变字典的容量。

参考:

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