📌  相关文章
📜  C#|获取OrderedDictionary中包含的键/值对的数量

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

OrderedDictionary.Count属性用于获取OrderedDictionary集合中包含的键/值对的数量。

句法:

public int Count { get; }

返回值: OrderedDictionary集合中包含的键/值对的数量。

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

范例1:

// C# code to get the number of
// key/values pairs contained
// in the 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");
  
        // To Get the number of key/values
        // pairs contained in the OrderedDictionary
        Console.WriteLine("Number of key/value pairs are : " 
                                            + myDict.Count);
    }
}

输出:

Number of key/value pairs are : 5

范例2:

// C# code to get the number of
// key/values pairs contained
// in the 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");
  
        // To Get the number of key/values
        // pairs contained in the OrderedDictionary
        Console.WriteLine("Number of key/value pairs are : " 
                                            + myDict.Count);
    }
}

输出:

Number of key/value pairs are : 4

参考:

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