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 StringCollection at the specified index of array in C#
The StringCollection.CopyTo() method in C# copies all elements from a StringCollection to a compatible array starting at the specified index. This method is particularly useful when you need to transfer string data from a collection to an array for further processing.
Syntax
Following is the syntax for the CopyTo() method −
public void CopyTo(string[] array, int index)
Parameters
-
array − The one-dimensional array that is the destination of the elements copied from StringCollection.
-
index − The zero-based index in the destination array at which copying begins.
Using CopyTo() with Index 0
Example
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringCollection strCol = new StringCollection();
String[] strArr = new String[] { "Tim", "Tom", "Sam", "Mark", "Katie", "Jacob"};
Console.WriteLine("Original Elements...");
for (int i = 0; i < strArr.Length; i++) {
Console.WriteLine(strArr[i]);
}
strCol.AddRange(strArr);
String[] arr = new String[strCol.Count];
strCol.CopyTo(arr, 0);
Console.WriteLine("Elements after copying StringCollection to array:");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine(arr[i]);
}
}
}
The output of the above code is −
Original Elements... Tim Tom Sam Mark Katie Jacob Elements after copying StringCollection to array: Tim Tom Sam Mark Katie Jacob
Using CopyTo() with Specified Index
Example
using System;
using System.Collections.Specialized;
public class Demo {
public static void Main(){
StringCollection strCol = new StringCollection();
String[] strArr = new String[] { "Tim", "Tom", "Sam", "Mark", "Katie", "Jacob", "David"};
Console.WriteLine("Original Elements...");
for (int i = 0; i < strArr.Length; i++) {
Console.WriteLine(strArr[i]);
}
strCol.AddRange(strArr);
String[] arr = new String[10];
strCol.CopyTo(arr, 3);
Console.WriteLine("Elements after copying starting at index 3:");
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine("Index " + i + ": " + (arr[i] ?? "null"));
}
}
}
The output of the above code is −
Original Elements... Tim Tom Sam Mark Katie Jacob David Elements after copying starting at index 3: Index 0: null Index 1: null Index 2: null Index 3: Tim Index 4: Tom Index 5: Sam Index 6: Mark Index 7: Katie Index 8: Jacob Index 9: David
Key Rules
-
The destination array must be large enough to accommodate all elements starting from the specified index.
-
The array must be one-dimensional and of type
string[]. -
If the index is negative or the array is too small, an exception will be thrown.
-
Elements before the specified index remain unchanged in the destination array.
Conclusion
The StringCollection.CopyTo() method provides an efficient way to copy all collection elements to an array starting at a specified index. This is useful for converting collection data to arrays while maintaining control over the placement within the destination array.
