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
Copying the entire ArrayList to 1-D Array starting at the specified index in C#
The ArrayList class in C# provides the CopyTo() method to copy all its elements to a one-dimensional array starting at a specified index. This method is useful when you need to transfer data from a dynamic ArrayList to a fixed-size array.
Syntax
Following is the syntax for the CopyTo() method −
public virtual void CopyTo(Array array, int arrayIndex)
Parameters
-
array − The one-dimensional array that is the destination of the elements copied from ArrayList.
-
arrayIndex − The zero-based index in the array at which copying begins.
How It Works
The CopyTo() method copies all ArrayList elements sequentially starting from the specified index in the target array. If the destination array has existing elements, they will be overwritten by the ArrayList elements, while remaining elements stay unchanged.
Using CopyTo() with String ArrayList
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList list = new ArrayList();
list.Add("PQ");
list.Add("RS");
list.Add("TU");
list.Add("UV");
list.Add("WX");
list.Add("YZ");
Console.WriteLine("ArrayList elements...");
for (int i = 0; i < list.Count; i++) {
Console.WriteLine(list[i]);
}
String[] strArr = new String[6] {"One", "Two", "Three", "Four", "Five", "Six"};
Console.WriteLine("\nArray elements...");
for (int i = 0; i < strArr.Length; i++) {
Console.WriteLine(strArr[i]);
}
list.CopyTo(strArr, 0);
Console.WriteLine("\nArray elements (updated)...");
for (int i = 0; i < strArr.Length; i++) {
Console.WriteLine(strArr[i]);
}
}
}
The output of the above code is −
ArrayList elements... PQ RS TU UV WX YZ Array elements... One Two Three Four Five Six Array elements (updated)... PQ RS TU UV WX YZ
Using CopyTo() with Integer ArrayList
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList list = new ArrayList();
list.Add(100);
list.Add(200);
Console.WriteLine("ArrayList elements...");
for (int i = 0; i < list.Count; i++) {
Console.WriteLine(list[i]);
}
int[] intArr = new int[5] {10, 20, 30, 40, 50};
Console.WriteLine("\nArray elements...");
for (int i = 0; i < intArr.Length; i++) {
Console.WriteLine(intArr[i]);
}
list.CopyTo(intArr, 0);
Console.WriteLine("\nArray elements (updated)...");
for (int i = 0; i < intArr.Length; i++) {
Console.WriteLine(intArr[i]);
}
}
}
The output of the above code is −
ArrayList elements... 100 200 Array elements... 10 20 30 40 50 Array elements (updated)... 100 200 30 40 50
Using CopyTo() with Different Starting Index
Example
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList list = new ArrayList();
list.Add("Alpha");
list.Add("Beta");
list.Add("Gamma");
Console.WriteLine("ArrayList elements...");
foreach (object item in list) {
Console.WriteLine(item);
}
string[] strArr = new string[6] {"A", "B", "C", "D", "E", "F"};
Console.WriteLine("\nOriginal array...");
foreach (string item in strArr) {
Console.WriteLine(item);
}
list.CopyTo(strArr, 2);
Console.WriteLine("\nArray after copying to index 2...");
foreach (string item in strArr) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
ArrayList elements... Alpha Beta Gamma Original array... A B C D E F Array after copying to index 2... A B Alpha Beta Gamma F
Conclusion
The CopyTo() method provides an efficient way to copy all ArrayList elements to a one-dimensional array starting at a specified index. This method overwrites existing elements in the target array while preserving elements outside the copy range, making it useful for data migration and array manipulation operations.
