Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get Synchronize access to the StringDictionary in C#
To get synchronize access to the StringDictionary, the code is as follows −
Example
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringDictionary strDict = new StringDictionary ();
strDict.Add("A", "Books");
strDict.Add("B", "Electronics");
strDict.Add("C", "Appliances");
strDict.Add("D", "Pet Supplies");
strDict.Add("E", "Clothing");
strDict.Add("F", "Footwear");
Console.WriteLine("StringDictionary key-value pairs...");
foreach(DictionaryEntry de in strDict) {
Console.WriteLine(de.Key + " " + de.Value);
}
Console.WriteLine("Value associated with key D = "+strDict["D"]);
Console.WriteLine("Value associated with key F = "+strDict["F"]);
Console.WriteLine("
Synchronize access..");
lock(strDict.SyncRoot) {
foreach(DictionaryEntry de in strDict) {
Console.WriteLine(de.Key + " " + de.Value);
}
}
}
}
Output
This will produce the following output −
StringDictionary key-value pairs... a Books b Electronics c Appliances d Pet Supplies e Clothing f Footwear Value associated with key D = Pet Supplies Value associated with key F = Footwear Synchronize access.. a Books b Electronics c Appliances d Pet Supplies e Clothing f Footwear
Example
Let us see another example −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
StringDictionary strDict = new StringDictionary();
strDict.Add("A", "John");
strDict.Add("B", "Andy");
strDict.Add("C", "Tim");
strDict.Add("D", "Ryan");
strDict.Add("E", "Kevin");
strDict.Add("F", "Katie");
strDict.Add("G", "Brad");
Console.WriteLine("StringDictionary elements...");
foreach(DictionaryEntry de in strDict) {
Console.WriteLine(de.Key + " " + de.Value);
}
Console.WriteLine("
Synchronize access..");
lock(strDict.SyncRoot) {
foreach(DictionaryEntry de in strDict) {
Console.WriteLine(de.Key + " " + de.Value);
}
}
}
}
Output
This will produce the following output −
StringDictionary elements... a John b Andy c Tim d Ryan e Kevin f Katie g Brad Synchronize access.. a John b Andy c Tim d Ryan e Kevin f Katie g Brad
Advertisements