Hashtable ht1 = new Hashtable(); ht1.Add(1,"One"); ht1.Add(2,"Two"); Hashtable ht2 = (Hashtable)ht1.Clone(); ht2.Add(3,"Three"); Console.WriteLine("Hashtable 1: "); foreach(DictionaryEntry de in ht1) Console.WriteLine($"{de.Key} : {de.Value}"); Console.WriteLine("Hashtable 2: "); foreach(DictionaryEntry de in ht2) Console.WriteLine($"{de.Key} : {de.Value}");
Hashtable 1: 1 : One 2 : Two Hashtable 2: 1 : One 2 : Two 3 : Three
Hashtable ht1 = new Hashtable(); ht1.Add(1,"One"); ht1.Add("Two",new List{2, 4, 6}); Hashtable ht2 = (Hashtable)ht1.Clone(); ((List )ht2["Two"]).Add(8); Console.WriteLine("Hashtable 1: "); foreach(DictionaryEntry de in ht1) { Console.Write($"{de.Key} : "); if(de.Value is List ) { foreach(int i in (List )de.Value) Console.Write($"{i} "); } else Console.Write($"{de.Value}"); Console.WriteLine(); } Console.WriteLine("Hashtable 2: "); foreach(DictionaryEntry de in ht2) { Console.Write($"{de.Key} : "); if(de.Value is List ) { foreach(int i in (List )de.Value) Console.Write($"{i} "); } else Console.Write($"{de.Value}"); Console.WriteLine(); }
Hashtable 1: 1 : One Two : 2 4 6 Hashtable 2: 1 : One Two : 2 4 6 8The Hashtable.Clone() method is part of the System.Collections package library.