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 StringDictionary to Array at the specified index in C#
The StringDictionary class in C# provides the CopyTo() method to copy its key-value pairs to a DictionaryEntry array starting at a specified index. This method is useful when you need to convert dictionary data into an array format for processing or manipulation.
Syntax
The syntax for copying a StringDictionary to an array at a specified index is −
stringDictionary.CopyTo(array, index);
Parameters
-
array − The destination
DictionaryEntry[]array where elements will be copied. - index − The zero-based index in the array where copying begins.
Using CopyTo() Starting at Index 0
This example demonstrates copying all StringDictionary entries to an array starting from index 0 −
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", "Convertible");
strDict.Add("10", "Quadricycle");
DictionaryEntry[] arr = new DictionaryEntry[strDict.Count];
strDict.CopyTo(arr, 0);
Console.WriteLine("StringDictionary copied to array:");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(arr[i].Key + " " + arr[i].Value);
}
}
}
The output of the above code is −
StringDictionary copied to array: 10 Quadricycle 1 SUV 2 AUV 3 Electric Car 4 Utility Vehicle 5 Hatchback 6 Compact car 7 MUV 8 Crossover 9 Convertible
Using CopyTo() Starting at Specified Index
This example shows copying StringDictionary entries to an array starting from index 2, leaving the first two positions empty −
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[strDict.Count + 2];
strDict.CopyTo(arr, 2);
Console.WriteLine("Array contents after copying at index 2:");
for (int i = 0; i < arr.Length; i++) {
if (arr[i].Key != null)
Console.WriteLine("Index " + i + ": " + arr[i].Key + " " + arr[i].Value);
else
Console.WriteLine("Index " + i + ": Empty");
}
}
}
The output of the above code is −
Array contents after copying at index 2: Index 0: Empty Index 1: Empty Index 2: 1 SUV Index 3: 2 AUV Index 4: 3 Electric Car Index 5: 4 Utility Vehicle Index 6: 5 Hatchback Index 7: 6 Compact car
Key Points
- The destination array must be of type
DictionaryEntry[]to hold both keys and values. - The array must have enough space from the specified index to accommodate all dictionary entries.
- StringDictionary stores keys in lowercase, so the order may differ from insertion order.
- If the array is too small, an
ArgumentExceptionwill be thrown.
Conclusion
The CopyTo() method of StringDictionary provides an efficient way to transfer dictionary contents to a DictionaryEntry array at any specified starting index. This is particularly useful for data processing scenarios where array operations are preferred over dictionary operations.
