- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
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
How to create a shallow copy of Hashtable in C#?
To create a shallow copy of 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", "BC"); hash.Add("3", "DE"); hash.Add("4", "EF"); hash.Add("5", "GH"); hash.Add("6", "IJ"); hash.Add("7", "KL"); hash.Add("8", "MN"); hash.Add("9", "OP"); hash.Add("10", "QR"); Console.WriteLine("Hashtable elements..."); foreach(DictionaryEntry d in hash) { Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value); } Hashtable hash2 = (Hashtable)hash.Clone(); Console.WriteLine("
Hashtable elements...cloned"); foreach(DictionaryEntry d in hash2) { Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value); } } }
Output
This will produce the following output −
Hashtable elements... Key = 10, Value = QR Key = 1, Value = AB Key = 2, Value = BC Key = 3, Value = DE Key = 4, Value = EF Key = 5, Value = GH Key = 6, Value = IJ Key = 7, Value = KL Key = 8, Value = MN Key = 9, Value = OP Hashtable elements...cloned Key = 10, Value = QR Key = 1, Value = AB Key = 2, Value = BC Key = 3, Value = DE Key = 4, Value = EF Key = 5, Value = GH Key = 6, Value = IJ Key = 7, Value = KL Key = 8, Value = MN Key = 9, Value = OP
Example
Let us see another example −
using System; using System.Collections; public class Demo { public static void Main() { Hashtable hash = new Hashtable(); hash.Add("A", "Electronics"); hash.Add("B", "Appliances"); hash.Add("C", "Pet Supplies"); hash.Add("D", "Books"); hash.Add("E", "Toys"); hash.Add("F", "Footwear"); hash.Add("G", "Clothing"); Console.WriteLine("Hashtable elements..."); foreach(DictionaryEntry d in hash) { Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value); } ICollection col = hash.Keys; Console.WriteLine("
Displaying only the keys..."); foreach(string str in col) Console.WriteLine(str + ": " + hash[str]); Hashtable hash2 = (Hashtable)hash.Clone(); Console.WriteLine("
Hashtable elements...cloned"); foreach(DictionaryEntry d in hash2) { Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value); } } }
Output
This will produce the following output −
Hashtable elements... Key = G, Value = Clothing Key = A, Value = Electronics Key = B, Value = Appliances Key = C, Value = Pet Supplies Key = D, Value = Books Key = E, Value = Toys Key = F, Value = Footwear Displaying only the keys... G: Clothing A: Electronics B: Appliances C: Pet Supplies D: Books E: Toys F: Footwear Hashtable elements...cloned Key = G, Value = Clothing Key = A, Value = Electronics Key = B, Value = Appliances Key = C, Value = Pet Supplies Key = D, Value = Books Key = E, Value = Toys Key = F, Value = Footwear
- Related Articles
- How to create a shallow copy of ArrayList in C#?
- How to create a shallow copy of the BitArray in C#?
- How to create a shallow copy of SortedList Object in C#?
- How to shallow copy the objects in JavaScript?
- How to Create a HashTable Collection in C#?
- Deep Copy and Shallow Copy in Java
- Return a shallow copy of IdentityHashMap in Java
- What is Shallow Copy and how it is different from Deep Copy in C#?
- Copy - Shallow and deep copy operations in Python
- How do you make a shallow copy of a list in Java?
- Python Shallow and Deep Copy operations
- What is the difference between shallow copy and deep copy in Java?
- How to create a copy of an object in PHP?
- What is shallow copy? Explain with an example in Java.
- How to get keys from a HashTable in C#?

Advertisements