C# C# Console Application

ArrayList CopyTo(Array) method in C#1 min read

.Net framework ArrayList.CopyTo(Array) method allow us to copy the entire ArrayList to a compatible one-dimensional Array, starting at the beginning of the target array. this arraylist CopyTo() method exists under System.Collections namespace.

this method require to pass a parameter named ‘array’. the ‘array’ parameter value type is System.Array which represents the one-dimensional Array that is the destination of the elements copied from ArrayList. the Array must have zero-based indexing.




the CopyTo() method has three exceptions. this method throw ArgumentNullException exception, if the ‘array’ is null. CopyTo() method throw ArgumentException exception, if the ‘array’ is multi-dimensional or the number of elements in the source arraylist is greater than the number of elements that the destination ‘array’ can contain. arraylist CopyTo() method throw InvalidCastException exception, if the type of the source arraylist cannot be cast automatically to the type of the destination array.

the following console c# example code demonstrate us how can we copy an arraylist to a compatible one dimensional array programmatically at run time in an console application.

C# Code:

 

Output:

 

Leave a Comment