CopyTo(Int32, Array, Int32, Int32) method in ArrayList
In this article I will discuss the CopyTo(Int32, Array, Int32, Int32) method of ArrayList.
Introduction
CopyTo(Int32, Array, Int32, Int32) method of ArrayList copies a range of elements from the ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array.In the following example I will explain the use of CopyTo method ,In this I will copy the entire arraylist into an array.This article also shows that how we convert the ArrayList to Array.
Example
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
ArrayList arr = new ArrayList();
arr.Add("Red");
arr.Add("Yellow");
arr.Add("Green");
arr.Add("Brown");
arr.Add("Pink");
Console.Write("{");
Console.WriteLine("The arraylist is as:");
foreach (string s in arr)
{
Console.Write(s + ",");
}
Console.Write("}");
Console.WriteLine();
Console.WriteLine();
string[] str = new string[10];
str[0] = "White";
str[1] = "Black";
str[2] = "Orange";
str[3] = "Gray";
str[4] = "Olive";
Console.WriteLine("The string array is as:");
Console.Write("{");
foreach (string s1 in str)
{
Console.Write(s1 + ",");
}
Console.Write("}");
Console.WriteLine();
Console.WriteLine();
//CopyTo(Int32,Array,Int32,Int32);
arr.CopyTo(0, str, 1, 3);
Console.WriteLine("The copied array is as:");
Console.Write("{");
foreach (string s in str)
{
Console.Write(s + ",");
}
Console.Write("}");
Console.WriteLine();
Console.WriteLine();
}
}
}
|
Output

Ask Your Question
Got a programming related question? You may want to post your question here