将Hashtable元素复制到C#中的数组实例
要将Hashtable元素复制到数组实例,代码如下-
示例
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");
Console.WriteLine("Hashtable Key and Value pairs...");
foreach(DictionaryEntry entry in hash){
Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
}
Console.WriteLine("Copied to Array Instance...");
DictionaryEntry[] dictArr = new DictionaryEntry[hash.Count];
hash.CopyTo(dictArr, 0);
for (int i = 0; i < dictArr.Length; i++)
Console.WriteLine("Key = "+dictArr[i].Key + ", Value = " + dictArr[i].Value);
}
}输出结果
这将产生以下输出-
Hashtable Key and Value pairs... 1 and AB 2 and CD 3 and EF 4 and GH 5 and IJ Copied to Array Instance... Key = 1, Value = AB Key = 2, Value = CD Key = 3, Value = EF Key = 4, Value = GH Key = 5, Value = IJ
示例
现在让我们来看另一个示例-
现场演示
using System;
using System.Collections;
public class Demo {
public static void Main(){
Hashtable hash = new Hashtable(5);
hash.Add("1", "AB");
hash.Add("2", "CD");
Console.WriteLine("Hashtable Key and Value pairs...");
foreach(DictionaryEntry entry in hash){
Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
}
Console.WriteLine("Copied to Array Instance...");
DictionaryEntry[] dictArr = new DictionaryEntry[5];
hash.CopyTo(dictArr, 2);
for (int i = 0; i < dictArr.Length; i++)
Console.WriteLine("Key = "+dictArr[i].Key + ", Value = " + dictArr[i].Value);
}
}输出结果
这将产生以下输出-
Hashtable Key and Value pairs... 1 and AB 2 and CD Copied to Array Instance... Key = , Value = Key = , Value = Key = 1, Value = AB Key = 2, Value = CD Key = , Value =
热门推荐
10 虎年文案简短祝福语长辈
11 新年过后奋斗祝福语简短
12 直播祝福语老师文案简短
13 女孩找工作祝福语简短
14 创业失败返乡祝福语简短
15 开工祝福语老师评语简短
16 老师订婚文案祝福语简短
17 赞美老师祝福语英文简短
18 爸爸过年的祝福语简短