如何在C#中创建Hashtable的浅表副本?
要创建Hashtable的浅表副本,代码如下-
示例
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("\nHashtable elements...cloned");
foreach(DictionaryEntry d in hash2) {
Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
}
}
}输出结果
这将产生以下输出-
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
示例
让我们看另一个例子-
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("\nDisplaying only the keys...");
foreach(string str in col)
Console.WriteLine(str + ": " + hash[str]);
Hashtable hash2 = (Hashtable)hash.Clone();
Console.WriteLine("\nHashtable elements...cloned");
foreach(DictionaryEntry d in hash2) {
Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);
}
}
}输出结果
这将产生以下输出-
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
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语