如何在 C# 中创建有序字典

原文:https://www . geeksforgeeks . org/how-to-create-ordered dictionary-in-c-sharp/

OrderedDictionary()构造函数用于初始化 OrderedDictionary 类的新实例,该实例将为空,并将具有默认的初始容量。orderedddictionary 类表示可由键或索引访问的键/值对的集合。它存在于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);
    }
}

Output:

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

Output:

Before Add Method: 0
After Add Method: 4

参考: