
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Creating a synchronized wrapper for the Hashtable in C#
To create a synchronized wrapper for the Hashtable, the code is as follows −
Example
using System; using System.Collections; public class Demo { public static void Main() { Hashtable hash = new Hashtable(); hash.Add("1", "AB"); hash.Add("2", "CD"); hash.Add("3", "EF"); hash.Add("4", "GH"); hash.Add("5", "IJ"); hash.Add("6", "KL"); Console.WriteLine("Hashtable elements..."); foreach(DictionaryEntry d in hash) { Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value); } Console.WriteLine("Hashtable is synchronized? = "+hash.IsSynchronized); } }
Output
This will produce the following output −
Hashtable elements... Key = 1, Value = AB Key = 2, Value = CD Key = 3, Value = EF Key = 4, Value = GH Key = 5, Value = IJ Key = 6, Value = KL Hashtable is synchronized? = False
Example
Let us see another example −
using System; using System.Collections; public class Demo { public static void Main() { Hashtable hash = new Hashtable(); hash.Add("1", "Mark"); hash.Add("2", "Gary"); hash.Add("3", "Jacob"); hash.Add("4", "Andy"); hash.Add("5", "Jack"); Console.WriteLine("Hashtable elements..."); foreach(DictionaryEntry d in hash) { Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value); } Console.WriteLine("Hashtable is synchronized? = "+hash.IsSynchronized); Hashtable hash2 = Hashtable.Synchronized(hash); Console.WriteLine("Hashtable is synchronized? = "+hash2.IsSynchronized); } }
Output
This will produce the following output −
Hashtable elements... Key = 1, Value = Mark Key = 2, Value = Gary Key = 3, Value = Jacob Key = 4, Value = Andy Key = 5, Value = Jack Hashtable is synchronized? = False Hashtable is synchronized? = True
- Related Articles
- Creating a synchronized wrapper for the ArrayList in C#
- Creating a synchronized wrapper for a SortedList object in C#
- Creating a read-only wrapper for the ArrayList in C#
- Check if Hashtable is synchronized C#
- Wrapper Clothing
- synchronized Keyword in Java
- Can a constructor be synchronized in Java?
- How to get hash code for the specified key of a Hashtable in C#?
- Check if a Hashtable is equal to another Hashtable in C#
- Clear a Hashtable in C#
- Explain wrapper classes in Java?
- Anonymous Wrapper Functions in JavaScript
- The HashTable Class in Javascript
- What are the considerations for creating a good Dividend Policy?
- Why do we need a wrapper class in Java?

Advertisements