How to create a StringDictionary in C#

StringDictionary() constructor is used to initialize a new instance of the StringDictionary class which will be empty and will have the default initial capacity. StringDictionary is a specialized collection. This class comes under the System.Collections.Specialized namespace. It only allows string keys and string values. It suffers from performance problems. It implements a hash table with the key and the value strongly typed to be strings rather than objects.

Syntax:

public StringDictionary ();

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to illustrate how
// to create a StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // sd is the StringDictionary object
        // StringDictionary() is the constructor
        // used to initializes a new
        // instance of the StringDictionary class
        StringDictionary sd = new StringDictionary();
  
        // Count property is used to get the
        // number of elements in StringDictionary
        // It will give 0 as no elements
        // are present currently
        Console.WriteLine(sd.Count);
    }
}

chevron_right


Output:

0

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to illustrate how
// to create a StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // sd is the StringDictionary object
        // StringDictionary() is the constructor
        // used to initializes a new
        // instance of the StringDictionary class
        StringDictionary sd = new StringDictionary();
  
        Console.Write("Before Add Method: ");
  
        // Count property is used to get the
        // number of elements in StringDictionary
        // It will give 0 as no elements
        // are present currently
        Console.WriteLine(sd.Count);
  
        // Adding key/value pairs in sd
        sd.Add("1", "C");
        sd.Add("2", "C++");
        sd.Add("3", "Java");
        sd.Add("4", "Python");
        sd.Add("5", "C#");
        sd.Add("6", "HTML");
  
        Console.Write("After Add Method: ");
  
        // Count property is used to get the
        // number of elements in sd
        Console.WriteLine(sd.Count);
    }
}

chevron_right


Output:

Before Add Method: 0
After Add Method: 6

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.