C# | Get the number of key/value pairs in the StringDictionary

StringDictionary.Count property is used to get the number of key/value pairs in the StringDictionary.

Syntax:

public virtual int Count { get; }

Return Value: It returns the number of key/value pairs in the StringDictionary.



Note: Retrieving the value of this property is an O(1) operation.

Below programs illustrate the use of StringDictionary.Count property:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to get the number of key/value
// pairs in the StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named myDict
        StringDictionary myDict = new StringDictionary();
  
        // Getting the number of key/value
        // pairs in the StringDictionary
        Console.Write("Number of key/value pairs in myDict are : ");
  
        Console.WriteLine(myDict.Count);
    }
}

chevron_right


Output:

Number of key/value pairs in myDict are : 0

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to get the number of key/value
// pairs in the StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named myDict
        StringDictionary myDict = new StringDictionary();
  
        // Adding key and value into the StringDictionary
        myDict.Add("G", "Geeks");
        myDict.Add("F", "For");
        myDict.Add("C", "C++");
        myDict.Add("DS", "Data Structures");
        myDict.Add("N", "Noida");
  
        // Getting the number of key/value
        // pairs in the StringDictionary
        Console.Write("Number of key/value pairs in myDict are : ");
  
        Console.WriteLine(myDict.Count);
    }
}

chevron_right


Output:

Number of key/value pairs in myDict are : 5

Reference:



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.