Open In App

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:




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

Output:
Number of key/value pairs in myDict are : 0

Example 2:




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

Output:
Number of key/value pairs in myDict are : 5

Reference:


Article Tags :
C#