Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Copy ListDictionary to Array instance at the specified index in C#
The ListDictionary class in C# provides the CopyTo method to copy its elements to an array at a specified index. This method is useful when you need to convert dictionary data into array format or merge dictionary elements with existing array data.
Syntax
Following is the syntax for the CopyTo method −
public void CopyTo(Array array, int index)
Parameters
array − The target array where elements will be copied. Must be of type
DictionaryEntry[].index − The zero-based index in the array at which copying begins.
Using CopyTo with Index 0
This example demonstrates copying a ListDictionary to the beginning of an array −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
ListDictionary dict = new ListDictionary();
dict.Add(1, "Harry");
dict.Add(2, "Mark");
dict.Add(3, "John");
dict.Add(4, "Jacob");
dict.Add(5, "Tim");
dict.Add(6, "Sam");
dict.Add(7, "Tom");
dict.Add(8, "Kevin");
Console.WriteLine("ListDictionary elements...");
foreach(DictionaryEntry d in dict){
Console.WriteLine(d.Key + " " + d.Value);
}
DictionaryEntry[] dictArr = new DictionaryEntry[dict.Count];
Console.WriteLine("\nCopying 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);
}
}
}
The output of the above code is −
ListDictionary elements... 1 Harry 2 Mark 3 John 4 Jacob 5 Tim 6 Sam 7 Tom 8 Kevin Copying to Array instance... Key = 1, Value = Harry Key = 2, Value = Mark Key = 3, Value = John Key = 4, Value = Jacob Key = 5, Value = Tim Key = 6, Value = Sam Key = 7, Value = Tom Key = 8, Value = Kevin
Using CopyTo with Specific Index
This example shows copying dictionary elements starting from index 5 in a larger array −
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo {
public static void Main() {
ListDictionary dict = new ListDictionary();
dict.Add(1, "Harry");
dict.Add(2, "Mark");
dict.Add(3, "John");
dict.Add(4, "Jacob");
dict.Add(5, "Tim");
Console.WriteLine("ListDictionary elements...");
foreach(DictionaryEntry d in dict) {
Console.WriteLine(d.Key + " " + d.Value);
}
DictionaryEntry[] dictArr = new DictionaryEntry[10];
Console.WriteLine("\nCopying to Array instance...");
dict.CopyTo(dictArr, 5);
for (int i = 0; i < dictArr.Length; i++) {
Console.WriteLine("Key = "+dictArr[i].Key + ", Value = " + dictArr[i].Value);
}
}
}
The output of the above code is −
ListDictionary elements... 1 Harry 2 Mark 3 John 4 Jacob 5 Tim Copying to Array instance... Key = , Value = Key = , Value = Key = , Value = Key = , Value = Key = , Value = Key = 1, Value = Harry Key = 2, Value = Mark Key = 3, Value = John Key = 4, Value = Jacob Key = 5, Value = Tim
Key Points
The target array must be large enough to accommodate all dictionary elements starting from the specified index.
Elements are copied as
DictionaryEntryobjects containing both key and value.Uninitialized array positions will contain default
DictionaryEntryvalues (empty key and value).The method preserves the original insertion order of the dictionary elements.
Conclusion
The CopyTo method of ListDictionary provides an efficient way to copy dictionary elements to an array starting at any specified index. This is particularly useful for merging dictionary data with existing arrays or converting collections for further processing.
