📜  C#|将HybridDictionary条目复制到阵列实例

📅  最后修改于: 2021-05-29 19:56:47             🧑  作者: Mango

HybridDictionary.CopyTo(Array,Int32)方法用于将HybridDictionary条目复制到指定索引处的一维Array实例。

句法:

public void CopyTo (Array array, int index);

参数:

例外情况:

  • ArgumentNullException:如果数组为null。
  • ArgumentOutOfRangeException:如果索引小于零。
  • InvalidCastException:如果源HybridDictionary的类型不能自动转换为目标数组的类型。
  • ArgumentException:如果array是多维的,或者源HybridDictionary中的元素数大于从arrayIndex到目标数组末尾的可用空间。

下面的程序说明了HybridDictionary.CopyTo(Array,Int32)方法的用法:

范例1:

// C# code to copy the HybridDictionary
// entries to a one-dimensional Array
// instance at the specified index.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary named myDict
        HybridDictionary myDict = new HybridDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("A", "Apple");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
        myDict.Add("E", "Elephant");
        myDict.Add("F", "Fish");
  
        // Creating a one-dimensional Array named myArr
        DictionaryEntry[] myArr = new DictionaryEntry[myDict.Count];
  
        // copying the HybridDictionary entries
        // to a one-dimensional Array instance
        // at the specified index
        myDict.CopyTo(myArr, 0);
  
        for (int i = 0; i < myArr.Length; i++)
            Console.WriteLine(myArr[i].Key + " --> " 
                                  + myArr[i].Value);
    }
}

输出:

A --> Apple
B --> Banana
C --> Cat
D --> Dog
E --> Elephant
F --> Fish

范例2:

// C# code to copy the HybridDictionary
// entries to a one-dimensional Array
// instance at the specified index.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary named myDict
        HybridDictionary myDict = new HybridDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("I", "first");
        myDict.Add("II", "second");
        myDict.Add("III", "third");
        myDict.Add("IV", "fourth");
        myDict.Add("V", "fifth");
  
        // Creating a one-dimensional Array named myArr
        DictionaryEntry[] myArr = new DictionaryEntry[myDict.Count];
  
        // copying the HybridDictionary entries
        // to a one-dimensional Array instance
        // at the specified index
        // This should raise "ArgumentOutOfRangeException"
        // as index is less than 0
        myDict.CopyTo(myArr, -2);
  
        for (int i = 0; i < myArr.Length; i++)
            Console.WriteLine(myArr[i].Key + " --> " 
                                  + myArr[i].Value);
    }
}

运行时错误:

笔记:

  • 元素将以枚举器通过HybridDictionary进行迭代的相同顺序复制到Array。
  • 要仅复制HybridDictionary中的键,请使用HybridDictionary.Keys.CopyTo
  • 要仅复制HybridDictionary中的值,请使用HybridDictionary.Values.CopyTo
  • 此方法是O(n)运算,其中n是Count。

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.specialized.hybriddictionary.copyto?view=netframework-4.7.2