📌  相关文章
📜  C#|将StringDictionary复制到指定索引处的Array

📅  最后修改于: 2021-05-29 13:20:06             🧑  作者: Mango

StringDictionary.CopyTo(Array,Int32)方法用于将字符串字典值复制到指定索引处的一维Array实例

句法:

public virtual void CopyTo (Array array, int index);

参数:

  • 数组:它是一维数组,是从StringDictionary复制的值的目的地。
  • index:它是复制开始的数组中的索引。

例外情况:

  • ArgumentNullException:如果键为null。
  • ArgumentOutOfRangeException:如果索引小于数组的下限。
  • ArgumentException:如果数组是多维的,或者StringDictionary中的元素数大于从索引到数组末尾的可用空间。

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

范例1:

// C# code to copy StringDictionary
// to Array at the specified index
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named myDict
        StringDictionary myDict = new StringDictionary();
  
        // Adding key and value into the StringDictionary
        myDict.Add("A", "Apple");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
        myDict.Add("E", "Elephant");
  
        // Creating an Array named myArr
        DictionaryEntry[] myArr = { new DictionaryEntry(),
                                    new DictionaryEntry(),
                                    new DictionaryEntry(),
                                    new DictionaryEntry(),
                                    new DictionaryEntry() };
  
        // Copying StringDictionary to
        // Array at the specified index
        myDict.CopyTo(myArr, 0);
  
        // Displaying key and value pairs in Array myArr
        for (int i = 0; i < myArr.Length; i++) {
            Console.WriteLine(myArr[i].Key + " " + myArr[i].Value);
        }
    }
}

输出:

d Dog
b Banana
c Cat
e Elephant
a Apple

范例2:

// C# code to copy StringDictionary
// to Array at the specified index
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named myDict
        StringDictionary myDict = new StringDictionary();
  
        // Adding key and value into the StringDictionary
        myDict.Add("A", "Apple");
        myDict.Add("B", "Banana");
        myDict.Add("C", "Cat");
        myDict.Add("D", "Dog");
        myDict.Add("E", "Elephant");
  
        // Creating an Array named myArr
        DictionaryEntry[] myArr = { new DictionaryEntry(),
                                    new DictionaryEntry(),
                                    new DictionaryEntry() };
  
        // Copying StringDictionary to
        // Array at the specified index
        // This should raise "ArgumentException" as
        // the number of elements in the StringDictionary
        // is greater than the available space from
        // index to the end of array.
        myDict.CopyTo(myArr, 0);
  
        // Displaying key and value pairs in Array myArr
        for (int i = 0; i < myArr.Length; i++) {
            Console.WriteLine(myArr[i].Key + " " + myArr[i].Value);
        }
    }
}

运行时错误:

笔记:

  • 复制到Array的元素的排序顺序与枚举器迭代StringDictionary的顺序相同。
  • 此方法是O(n)运算,其中n是Count。

参考:

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