C# StringDictionary Class
Last Updated :
06 Feb, 2025
In C#, the StringDictionary class is the part of the System.Collections.Specialized namespace. It is a collection of key-value pairs where the keys are strings and values are objects. It is similar to a Hashtable. But it is specifically designed for use with string keys.
- Keys are always strings, and values can be any object.
- It does not maintain the order of insertion of elements.
- Provides indexed access to values based on string keys.
- By default, it is case-sensitive.
Example: This example demonstrates how to use the StringDictionary class to store and iterate over key-value pairs where keys are strings and values can be any object.
C#
// C# program to demonstrate StringDictionary class
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks
{
static void Main()
{
// Create a StringDictionary
StringDictionary sd = new StringDictionary();
// Insert key-value pairs
sd.Add("1", "Geek1");
sd.Add("2", "Geek2");
sd.Add("3", "Geek3");
sd.Add("4", "Geek4");
// Iterate through the StringDictionary
// and print the key-value pairs
foreach (DictionaryEntry i in sd)
{
Console.WriteLine($"{i.Key} --> {i.Value}");
}
}
}
Output3 --> Geek3
4 --> Geek4
2 --> Geek2
1 --> Geek1
Declaration of StringDictionary
In C#, the StringDictionary is declared as:
StringDictionary dictionary = new StringDictionary();
Constructor
StringDictionary(): Initializes a new instance of the StringDictionary class.
Example: This example demonstrates how to create a StringDictionary, add integer values as string, and iterate through the dictionary to display the keys and their corresponding values.
C#
// C# program to demonstrates how to
// create a StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main()
{
// Creating a StringDictionary
StringDictionary sd = new StringDictionary();
// Adding key and value into the StringDictionary
sd.Add("X", "100");
sd.Add("Y", "200");
sd.Add("Z", "300");
// Displaying the keys and values in StringDictionary
foreach(DictionaryEntry i in sd)
{
Console.WriteLine(i.Key + " --> " + i.Value);
}
}
}
Outputx --> 100
z --> 300
y --> 200
Properties
Properties | Description |
---|
Count | Gets the number of key/value pairs in the StringDictionary. |
IsSynchronized | Gets a value indicating whether access to the StringDictionary is synchronized (thread safe). |
Item[String] | Gets or sets the value associated with the specified key. |
Keys | Gets a collection of keys in the StringDictionary. |
SyncRoot | Gets an object that can be used to synchronize access to the StringDictionary. |
Values | Gets a collection of values in the StringDictionary. |
Example: This example demonstrates how to use Values property to access that values in StringDictionary and check its IsSynchronized property to determine if its thread-safe.
C#
// C# program to demonstrates the working of
// values and IsSynchronized property
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks{
public static void Main() {
// Creating a StringDictionary
StringDictionary sd = new StringDictionary();
// Adding key and value
sd.Add("1", "One");
sd.Add("5", "Five");
sd.Add("7", "Seven");
sd.Add("10", "Ten");
// Values Property
// Getting a collection of values in the StringDictionary
foreach (string i in sd.Values) {
Console.WriteLine(i);
}
// IsSynchronized Property
// Checking if StringDictionary is synchronized(thread-safe)
Console.WriteLine(sd.IsSynchronized);
}
}
OutputFive
Seven
Ten
One
False
Methods
Methods | Description |
---|
Add(String, String) | Adds an entry with the specified key and value into the StringDictionary. |
Clear() | Removes all entries from the StringDictionary. |
ContainsKey(String) | Determines if the StringDictionary contains a specific key. |
ContainsValue(String) | Determines if the StringDictionary contains a specific value. |
CopyTo(Array, Int32) | Copies the string dictionary values to a one-dimensional Array instance at the specified index. |
Equals(Object) | Determines whether the specified object is equal to the current object. |
GetEnumerator() | Returns an enumerator that iterates through the string dictionary. |
GetHashCode() | Serves as the default hash function. |
GetType() | Gets the Type of the current instance. |
MemberwiseClone() | Creates a shallow copy of the current Object. |
Remove(String) | Removes the entry with the specified key from the string dictionary. |
ToString() | Returns a string that represents the current object. |
Example 1: This example demonstrates how to use the Count property to get the number of key-value pairs and how to remove key-value pair using the Remove() method.
C#
// C# code to remove the entry
// with the specified key from
// the StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main()
{
// Creating a StringDictionary
StringDictionary sd = new StringDictionary();
// Adding key and value into the StringDictionary
sd.Add("X", "100");
sd.Add("Y", "200");
sd.Add("Z", "300");
// Displaying the keys and values in StringDictionary
Console.WriteLine("The number of key-value pairs are : " + sd.Count);
// Removing the entry with the specified
// key from the StringDictionary
sd.Remove("Y");
// Displaying the keys and values in StringDictionary
Console.WriteLine("The number of key-value pairs are : " + sd.Count);
}
}
OutputThe number of key-value pairs are : 3
The number of key-value pairs are : 2
Example 2: This example demonstrates how to use the CopyTo method to copy the contents of a StringDicitonary into an array and then display the key-value pairs from the array.
C#
// C# code to copy StringDictionary
// to Array at the specified index
using System;
using System.Collections;
using System.Collections.Specialized;
class Geeks {
public static void Main()
{
// Creating a StringDictionary
StringDictionary sd = new StringDictionary();
// Adding key and value into the StringDictionary
sd.Add("1", "Geeks");
sd.Add("2", "for");
sd.Add("3", "Geeks");
// Creating an Array
DictionaryEntry[] arr = { new DictionaryEntry(),
new DictionaryEntry(),
new DictionaryEntry() };
// Copying StringDictionary to
// Array at the specified index
sd.CopyTo(arr, 0);
// Displaying key and value pairs in Array
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(arr[i].Key + " --> "
+ arr[i].Value);
}
}
}
Output3 --> Geeks
2 --> for
1 --> Geeks
Similar Reads
C# Tutorial C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 min read
Introduction to .NET Framework The .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into
6 min read
C# Interview Questions and Answers C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications.Her
15+ min read
C# Dictionary Dictionary in C# is a generic collection that stores key-value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of a Dictionary is, that it is a generic type. A dictionary is defined under System.Collections.Generic namespace. It is dynamic in nature mean
5 min read
C# List Class In C#, the List<T> class represents the list of objects that can be accessed by index. It comes under the System.Collections.Generic namespace. List class can be used to create a collection of different types like integers, strings, etc. List<T> class also provides the methods to search,
7 min read
C# Delegates A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. It provides a way which tells which method is to be called when an event is triggered. For example, if you click on a Button on a form (Windows Form application),
6 min read
ASP.NET Interview Questions and Answer ASP.NET is a popular framework by Microsoft for building fast and scalable web applications. It allows developers to create dynamic websites, services, and apps, using server-side code and offering a user-friendly experience. Trusted by companies like Microsoft, Dell, and Accenture, ASP.NET is used
15+ min read
C# .NET Framework (Basic Architecture and Component Stack) C# (C-Sharp) is a modern, object-oriented programming language developed by Microsoft in 2000. It is a part of the .NET ecosystem and is widely used for building desktop, web, mobile, cloud, and enterprise applications. This is originally tied to the .NET Framework, C# has evolved to be the primary
6 min read
C# Data Types Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C# each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language and all constants or variables defined for a given pr
7 min read
C# Arrays An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read