Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

การคัดลอกข้อมูล HybridDictionary ไปยังอาร์เรย์ใน C#

ภาพรวม

HybridDictionary.CopyTo(Array array, int index) คัดลอกคู่คีย์/ค่าเป็น DictionaryEntry ไปยังอาร์เรย์ปลายทาง ตั้งแต่ดัชนีที่ระบุ

ตัวอย่างที่ 1: คัดลอกไปอาร์เรย์ขนาด 10 เริ่มดัชนี 2

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo
{
    public static void Main()
    {
        HybridDictionary dict = new HybridDictionary();
        dict.Add("1", "SUV");
        dict.Add("2", "AUV");
        dict.Add("3", "Utility Vehicle");
        dict.Add("4", "MUV");
        dict.Add("5", "Compact Car");
        dict.Add("6", "Convertible");

        Console.WriteLine("HybridDictionary Key and Value pairs...");
        foreach (DictionaryEntry entry in dict)
        {
            Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
        }

        DictionaryEntry[] dictArr = new DictionaryEntry[10];
        Console.WriteLine("\nCopied to Array Instance...");
        dict.CopyTo(dictArr, 2);

        for (int i = 0; i < dictArr.Length; i++)
        {
            Console.WriteLine("Key = " + dictArr[i].Key + ", Value = " + dictArr[i].Value);
        }
    }
}

ผลลัพธ์

HybridDictionary Key and Value pairs...
1 and SUV
2 and AUV
3 and Utility Vehicle
4 and MUV
5 and Compact Car
6 and Convertible

Copied to Array Instance...
Key = , Value = 
Key = , Value = 
Key = 1, Value = SUV
Key = 2, Value = AUV
Key = 3, Value = Utility Vehicle
Key = 4, Value = MUV
Key = 5, Value = Compact Car
Key = 6, Value = Convertible
Key = , Value = 
Key = , Value = 

ตัวอย่างที่ 2: คัดลอกไปอาร์เรย์ขนาด 10 เริ่มดัชนี 0 (มีการกำหนดความจุเริ่มต้น)

using System;
using System.Collections;
using System.Collections.Specialized;

public class Demo
{
    public static void Main()
    {
        HybridDictionary dict = new HybridDictionary(10);
        dict.Add("1", "SUV");
        dict.Add("2", "AUV");
        dict.Add("3", "Utility Vehicle");
        dict.Add("4", "MUV");

        Console.WriteLine("HybridDictionary Key and Value pairs...");
        foreach (DictionaryEntry entry in dict)
        {
            Console.WriteLine("{0} and {1} ", entry.Key, entry.Value);
        }

        DictionaryEntry[] dictArr = new DictionaryEntry[10];
        Console.WriteLine("\nCopied 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);
        }
    }
}

ผลลัพธ์

HybridDictionary Key and Value pairs...
1 and SUV
2 and AUV
3 and Utility Vehicle
4 and MUV

Copied to Array Instance...
Key = 1, Value = SUV
Key = 2, Value = AUV
Key = 3, Value = Utility Vehicle
Key = 4, Value = MUV
Key = , Value = 
Key = , Value = 
Key = , Value = 
Key = , Value = 
Key = , Value = 
Key = , Value = 

สรุป

  • อาร์เรย์ปลายทางต้องเป็น DictionaryEntry[]
  • ดัชนีเริ่มต้นกำหนดตำแหน่งที่จะเริ่มเขียนข้อมูล
  • ช่องว่างในอาร์เรย์จะเป็นค่า default (null/empty)