将ListDictionary复制到C#中指定索引处的Array实例
要将ListDictionary复制到指定索引处的Array实例,代码如下-
示例
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
ListDictionary dict = new ListDictionary();
dict.Add(1, "Harry");
dict.Add(2, "Mark");
dict.Add(3, "John");
dict.Add(4, "Jacob");
dict.Add(5, "Tim");
dict.Add(6, "Sam");
dict.Add(7, "Tom");
dict.Add(8, "Kevin");
Console.WriteLine("ListDictionary elements...");
foreach(DictionaryEntry d in dict){
Console.WriteLine(d.Key + " " + d.Value);
}
DictionaryEntry[] dictArr = new DictionaryEntry[dict.Count];
Console.WriteLine("\nCopying to Array instance...");
dict.CopyTo(dictArr, 0);
for (int i = 0; i < dictArr.Length; i++) {
Console.WriteLine("Key = "+dictArr[i].Key + ", Value = " + dictArr[i].Value);
}
}
}输出结果
这将产生以下输出-
ListDictionary elements... 1 Harry 2 Mark 3 John 4 Jacob 5 Tim 6 Sam 7 Tom 8 Kevin Copying to Array instance... Key = 1, Value = Harry Key = 2, Value = Mark Key = 3, Value = John Key = 4, Value = Jacob Key = 5, Value = Tim Key = 6, Value = Sam Key = 7, Value = Tom Key = 8, Value = Kevin
示例
现在让我们来看另一个示例-
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict = new ListDictionary();
dict.Add(1, "Harry");
dict.Add(2, "Mark");
dict.Add(3, "John");
dict.Add(4, "Jacob");
dict.Add(5, "Tim");
Console.WriteLine("ListDictionary elements...");
foreach(DictionaryEntry d in dict) {
Console.WriteLine(d.Key + " " + d.Value);
}
DictionaryEntry[] dictArr = new DictionaryEntry[10];
Console.WriteLine("\nCopying to Array instance...");
dict.CopyTo(dictArr, 5);
for (int i = 0; i < dictArr.Length; i++) {
Console.WriteLine("Key = "+dictArr[i].Key + ", Value = " + dictArr[i].Value);
}
}
}输出结果
这将产生以下输出-
ListDictionary elements... 1 Harry 2 Mark 3 John 4 Jacob 5 Tim Copying to Array instance... Key = , Value = Key = , Value = Key = , Value = Key = , Value = Key = , Value = Key = 1, Value = Harry Key = 2, Value = Mark Key = 3, Value = John Key = 4, Value = Jacob Key = 5, Value = Tim
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿