📌  相关文章
📜  如何在C#中创建ListDictionary

📅  最后修改于: 2021-05-30 00:07:42             🧑  作者: Mango

ListDictionary()构造函数用于使用默认比较器初始化ListDictionary类的新的空实例。 ListDictionary是一个专门的集合。它位于System.Collections.Specialized命名空间下。此类型表示非通用词典类型。它通过链表实现。此类是针对小型列表的字典集合( System.Collections.IDictionary

句法:

public ListDictionary ();

范例1:

// C# Program to illustrate how
// to create a ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // ld is the ListDictionary object
        // ListDictionary() is the constructor
        // used to initializes a new
        // instance of the ListDictionary class
        ListDictionary ld = new ListDictionary();
  
        // Count property is used to get the
        // number of elements in ListDictionary
        // It will give 0 as no elements
        // are present currently
        Console.WriteLine("The number of elements: "+ 
                                           ld.Count);
    }
}
输出:
The number of elements: 0

范例2:

// C# Program to illustrate how
// to create a ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // ld is the ListDictionary object
        // ListDictionary() is the constructor
        // used to initializes a new
        // instance of the ListDictionary class
        ListDictionary ld = new ListDictionary();
  
        Console.Write("Before Add Method: ");
  
        // Count property is used to get the
        // number of elements in ListDictionary
        // It will give 0 as no elements
        // are present currently
        Console.WriteLine(ld.Count);
  
        // Adding key/value pairs in ld
        ld.Add("Australia", "Canberra");
        ld.Add("Belgium", "Brussels");
        ld.Add("Netherlands", "Amsterdam");
        ld.Add("China", "Beijing");
        ld.Add("Russia", "Moscow");
        ld.Add("India", "New Delhi");
  
        Console.Write("After Add Method: ");
  
        // Count property is used to get the
        // number of elements in ld
        Console.WriteLine(ld.Count);
    }
}
输出:
Before Add Method: 0
After Add Method: 6

参考:

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