Open In App

C# | Get the number of key/values pairs contained in OrderedDictionary

OrderedDictionary.Count property is used to get the number of key/values pairs contained in the OrderedDictionary collection.

Syntax:



public int Count { get; }

Return Value: The number of key/value pairs contained in the OrderedDictionary collection.

Below given are some examples to understand the implementation in a better way:



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

Output:

Number of key/value pairs are : 5

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

Output:

Number of key/value pairs are : 4

Reference:


Article Tags :
C#