StringDictionary Class in C#?

CsharpServer Side ProgrammingProgramming

The StringDictionay class implements a hash table with the key and the value strongly typed to be strings rather than objects.

Following are the properties of the StringDictionary class −

Sr.NoProperty & Description
1Count
Gets the number of key/value pairs in the StringDictionary.
2IsSynchronized
Gets a value indicating whether access to the StringDictionary is synchronized (thread safe).
3tem[String]
Gets or sets the value associated with the specified key.
4Keys
Gets a collection of keys in the StringDictionary..
5SyncRoot
Gets an object that can be used to synchronize access to the StringDictionary.
6Values
Gets a collection of values in the StringDictionary.

Following are some of the methods of the StringDictionary class −

Sr.NoMethods & Description
1Add(String, String)
Adds an entry with the specified key and value into the StringDictionary.
2Clear()
Removes all entries from the StringDictionary.
3ContainsKey(String)
Determines if the StringDictionary contains a specific
4ContainsValue(String)
Determines if the StringDictionary contains a specific value.
5CopyTo(Array, Int32)
Copies the string dictionary values to a onedimensional Array instance at the specified index.
6Equals(Object)
Determines whether the specified object is equal to the current object.(Inherited from Object)

Example

Let us now see some examples −

To check if two StringDictionary objects are equal or not, the code is as follows −

 Live Demo

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "John");
      strDict1.Add("B", "Andy");
      strDict1.Add("C", "Tim");
      strDict1.Add("D", "Ryan");
      strDict1.Add("E", "Kevin");
      strDict1.Add("F", "Katie");
      strDict1.Add("G", "Brad");
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("A", "John");
      strDict2.Add("B", "Andy");
      strDict2.Add("C", "Tim");
      strDict2.Add("D", "Ryan");
      strDict2.Add("E", "Kevin");
      strDict2.Add("F", "Katie");
      strDict2.Add("G", "Brad");
      StringDictionary strDict3 = new StringDictionary();
      strDict3 = strDict2;
      Console.WriteLine("Is Dictionary2 equal to Dictionary3? = "+strDict2.Equals(strDict3));
      Console.WriteLine("Is Dictionary1 equal to Dictionary3? = "+strDict1.Equals(strDict3));
   }
}

Output

This will produce the following output −

Is Dictionary2 equal to Dictionary3? = True
Is Dictionary1 equal to Dictionary3? = False

Example

To check if StringDictionary is synchronized, the code is as follows −

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringDictionary strDict1 = new StringDictionary();
      strDict1.Add("A", "John");
      strDict1.Add("B", "Andy");
      strDict1.Add("C", "Tim");
      strDict1.Add("D", "Ryan");
      strDict1.Add("E", "Kevin");
      strDict1.Add("F", "Katie");
      strDict1.Add("G", "Brad");
      Console.WriteLine("StringDictionary1 elements...");
      foreach(DictionaryEntry de in strDict1) {
         Console.WriteLine(de.Key + " " + de.Value);
      }
      StringDictionary strDict2 = new StringDictionary();
      strDict2.Add("1", "A");
      strDict2.Add("2", "B");
      strDict2.Add("3", "C");
      strDict2.Add("4", "D");
      strDict2.Add("5", "E");
      Console.WriteLine("\nStringDictionary2 key-value pairs...");
      IEnumerator demoEnum = strDict2.GetEnumerator();
      DictionaryEntry d;
      while (demoEnum.MoveNext()) {
         d = (DictionaryEntry)demoEnum.Current;
         Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
      }
      Console.WriteLine("Is the StringDictionary2 synchronized? = "+strDict2.IsSynchronized);
   }
}

Output

This will produce the following output −

StringDictionary1 elements...
a John
b Andy
c Tim
d Ryan
e Kevin
f Katie
g Brad
StringDictionary2 key-value pairs...
Key = 1, Value = A
Key = 2, Value = B
Key = 3, Value = C
Key = 4, Value = D
Key = 5, Value = E
Is the StringDictionary2 synchronized? = False
raja
Published on 16-Dec-2019 06:48:57
Advertisements