📜  C#|从ListDictionary中删除所有条目

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

ListDictionary.Clear方法用于从ListDictionary中删除所有条目。

句法:

public void Clear ();

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

范例1:

// C# code to remove all entries
// 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);
        }
  
        // Removing all entries from the ListDictionary
        myDict.Clear();
  
        // 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 : 0
The key/value pairs in myDict are :

范例2:

// C# code to remove all entries
// 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);
        }
  
        // Removing all entries from the ListDictionary
        myDict.Clear();
  
        // 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 : 5
The key/value pairs in myDict are : 
I first
II second
III third
IV fourth
V fifth
Total key/value pairs in myDict are : 0
The key/value pairs in myDict are :

笔记:

  • Count设置为零,并且还会释放对集合元素中其他对象的引用。
  • 此方法是O(1)操作。

参考:

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