C#中将OrderedDictionary元素复制到指定索引处的Array实例

csharpserver side programmingprogramming更新于 2025/10/7 5:27:00

将OrderedDictionary元素复制到指定索引处的Array实例,代码如下 −

示例

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      OrderedDictionary dict = new OrderedDictionary ();
      dict.Add(1, "Harry");
      dict.Add(2, "Mark");
      dict.Add(3, "John");
      dict.Add(4, "Jacob");
      dict.Add(5, "Tim");
      Console.WriteLine("OrderedDictionary 元素...");
      foreach(DictionaryEntry d in dict){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      DictionaryEntry[] dictArr = new DictionaryEntry[dict.Count];
      Console.WriteLine("
复制到数组实例...");       dict.CopyTo(dictArr, 0);       for (int i = 0; i < dictArr.Length; i++) {          Console.WriteLine("Key = "+dictArr[i].Key + ", Value = " + dictArr[i].Value);       }    } }

输出

这将产生以下输出 −

OrderedDictionary 元素...
1 Harry
2 Mark
3 John
4 Jacob
5 Tim
复制到数组实例...
Key = 5, Value = Tim
Key = 4, Value = Jacob
Key = 3, Value = John
Key = 2, Value = Mark
Key = 1, Value = Harry

示例

Let us now see another example:
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      OrderedDictionary dict = new OrderedDictionary ();
      dict.Add(1, 10);
      dict.Add(2, 20);
      Console.WriteLine("OrderedDictionary 元素...");
      foreach(DictionaryEntry d in dict){
         Console.WriteLine(d.Key + " " + d.Value);
      }
      DictionaryEntry[] dictArr = new DictionaryEntry[5];
      Console.WriteLine("
复制到数组实例...");       dict.CopyTo(dictArr, 0);       for (int i = 0; i < dictArr.Length; i++) {          Console.WriteLine("Key = "+dictArr[i].Key + ", Value = " + dictArr[i].Value);       }    } }

输出

这将产生以下输出 −

OrderedDictionary 元素...
1 10
2 20

复制到数组实例...
Key = 2, Value = 20
Key = 1, Value = 10
Key = , Value =
Key = , Value =
Key = , Value =

相关文章