- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHPPhysics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
StringDictionary Class in C#?
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.No | Property & Description |
|---|---|
| 1 |
Count Gets the number of key/value pairs in the StringDictionary. |
| 2 |
IsSynchronized Gets a value indicating whether access to the StringDictionary is synchronized (thread safe). |
| 3 |
tem[String] Gets or sets the value associated with the specified key. |
| 4 |
Keys Gets a collection of keys in the StringDictionary.. |
| 5 |
SyncRoot Gets an object that can be used to synchronize access to the StringDictionary. |
| 6 |
Values Gets a collection of values in the StringDictionary. |
Following are some of the methods of the StringDictionary class −
| Sr.No | Methods & Description |
|---|---|
| 1 |
Add(String, String) Adds an entry with the specified key and value into the StringDictionary. |
| 2 |
Clear() Removes all entries from the StringDictionary. |
| 3 |
ContainsKey(String) Determines if the StringDictionary contains a specific |
| 4 |
ContainsValue(String) Determines if the StringDictionary contains a specific value. |
| 5 |
CopyTo(Array, Int32) Copies the string dictionary values to a onedimensional Array instance at the specified index. |
| 6 |
Equals(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 −
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("
StringDictionary2 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
Advertisements