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

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

OrderedDictionary()构造函数用于初始化OrderedDictionary类的新实例,该实例将为空并具有默认的初始容量。 OrderedDictionary类表示键/索引可访问的键/值对的集合。它存在于System.Collections.Specialized命名空间中。

句法:

public OrderedDictionary();

范例1:

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

范例2:

// C# Program to illustrate how
// to create a OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // od is the OrderedDictionary object
        // OrderedDictionary() is the constructor
        // used to initializes a new
        // instance of the OrderedDictionary class
        OrderedDictionary od = new OrderedDictionary();
  
        Console.Write("Before Add Method: ");
  
        // Count property is used to get the
        // number of elements in OrderedDictionary
        // It will give 0 as no elements
        // are present currently
        Console.WriteLine(od.Count);
  
        // Adding key/value pairs in od
        od.Add("1", "C");
        od.Add("2", "C++");
        od.Add("3", "Java");
        od.Add("4", "C#");
  
        Console.Write("After Add Method: ");
  
        // Count property is used to get the
        // number of elements in ld
        Console.WriteLine(od.Count);
    }
}
输出:
Before Add Method: 0
After Add Method: 4

参考:

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