Open In App

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

Last Updated : 01 Feb, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads