將 StringDictionary 複製到 C# 中指定索引處的陣列


要將 StringDictionary 複製到指定索引處的陣列,程式碼如下 −

示例

 執行演示

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      StringDictionary strDict = new StringDictionary();
      strDict.Add("1", "SUV");
      strDict.Add("2", "AUV");
      strDict.Add("3", "Electric Car");
      strDict.Add("4", "Utility Vehicle");
      strDict.Add("5", "Hatchback");
      strDict.Add("6", "Compact car");
      strDict.Add("7", "MUV");
      strDict.Add("8", "Crossover");
      strDict.Add("9", "Covertible");
      strDict.Add("10", "Quadricycle");
      DictionaryEntry[] arr = { new DictionaryEntry(),
         new DictionaryEntry(),
         new DictionaryEntry(),
         new DictionaryEntry(),
         new DictionaryEntry(),
         new DictionaryEntry(),
         new DictionaryEntry(),
         new DictionaryEntry(),
         new DictionaryEntry(),
         new DictionaryEntry()};
      strDict.CopyTo(arr, 0);
      for (int i = 0; i < arr.Length; i++) {
         Console.WriteLine(arr[i].Key + " " + arr[i].Value);
      }
   }
}

輸出

這將產生以下輸出 −

10 Quadricycle
1 SUV
2 AUV
3 Electric Car
4 Utility Vehicle
5 Hatchback
6 Compact car
7 MUV
8 Crossover
9 Covertible

示例

現在讓我們看另一個示例 −

 執行演示

using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
   public static void Main(){
      StringDictionary strDict = new StringDictionary();
      strDict.Add("1", "SUV");
      strDict.Add("2", "AUV");
      strDict.Add("3", "Electric Car");
      strDict.Add("4", "Utility Vehicle");
      strDict.Add("5", "Hatchback");
      strDict.Add("6", "Compact car");
      DictionaryEntry[] arr = { new DictionaryEntry(),
         new DictionaryEntry(),
         new DictionaryEntry(),
         new DictionaryEntry(),
         new DictionaryEntry(),
         new DictionaryEntry(),
         new DictionaryEntry(),
         new DictionaryEntry()};
      strDict.CopyTo(arr, 2);
      for (int i = 0; i < arr.Length; i++) {
         Console.WriteLine(arr[i].Key + " " + arr[i].Value);
      }
   }
}

輸出

這將產生以下輸出 −

1 SUV
2 AUV
3 Electric Car
4 Utility Vehicle
5 Hatchback
6 Compact car

更新於: 10-Dec-2019

76 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.