C# | StringDictionary Class
Last Updated :
24 Jan, 2019
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.
Characteristics:
- A key cannot be null, but a value can.
- The key is handled in a case-insensitive manner i.e, it is translated to lowercase before it is used with the string dictionary.
- The constructor StringDictionary() initializes a new instance of the StringDictionary class.
Constructors
Constructor |
Description |
StringDictionary() |
Initializes a new instance of the StringDictionary class. |
Example:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringDictionary myDict = new StringDictionary();
myDict.Add( "A" , "Apple" );
myDict.Add( "B" , "Banana" );
myDict.Add( "C" , "Cat" );
myDict.Add( "D" , "Dog" );
myDict.Add( "E" , "Elephant" );
foreach (DictionaryEntry dic in myDict)
{
Console.WriteLine(dic.Key + " " + dic.Value);
}
}
}
|
Output:
d Dog
b Banana
c Cat
e Elephant
a Apple
Properties
Property |
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:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringDictionary myDict = new StringDictionary();
myDict.Add( "3" , "prime & odd" );
myDict.Add( "2" , "prime & even" );
myDict.Add( "4" , "non-prime & even" );
myDict.Add( "9" , "non-prime & odd" );
foreach ( string val in myDict.Values)
{
Console.WriteLine(val);
}
Console.WriteLine(myDict.IsSynchronized);
}
}
|
Output:
prime & even
prime & odd
non-prime & odd
non-prime & even
False
Methods
Method |
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:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringDictionary myDict = new StringDictionary();
myDict.Add( "A" , "Apple" );
myDict.Add( "B" , "Banana" );
myDict.Add( "C" , "Cat" );
myDict.Add( "D" , "Dog" );
myDict.Add( "E" , "Elephant" );
myDict.Add( "F" , "Fish" );
Console.WriteLine( "The number of key/value pairs are : " + myDict.Count);
myDict.Remove( "D" );
Console.WriteLine( "The number of key/value pairs are : " + myDict.Count);
}
}
|
Output:
The number of key/value pairs are : 6
The number of key/value pairs are : 5
Example 2:
using System;
using System.Collections;
using System.Collections.Specialized;
class GFG {
public static void Main()
{
StringDictionary myDict = new StringDictionary();
myDict.Add( "A" , "Apple" );
myDict.Add( "B" , "Banana" );
myDict.Add( "C" , "Cat" );
myDict.Add( "D" , "Dog" );
myDict.Add( "E" , "Elephant" );
DictionaryEntry[] myArr = { new DictionaryEntry(),
new DictionaryEntry(),
new DictionaryEntry(),
new DictionaryEntry(),
new DictionaryEntry() };
myDict.CopyTo(myArr, 0);
for ( int i = 0; i < myArr.Length; i++) {
Console.WriteLine(myArr[i].Key + " " + myArr[i].Value);
}
}
}
|
Output:
d Dog
b Banana
c Cat
e Elephant
a Apple
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...