StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace.
StringCollection.CopyTo(String, Int32) method is used to copy the entire StringCollection values to a one-dimensional array of strings which starts at the specified index of the target array.
public void CopyTo (string array, int index);
- array : It is the one-dimensional array of strings that is the destination of the elements copied from StringCollection. The Array must have zero-based indexing.
- index : It is the zero-based index in array at which copying begins.
- ArgumentNullException : If the array is null.
- ArgumentOutOfRangeException : If the index is less than zero.
- InvalidCastException : If the type of the source StringCollection cannot be cast automatically to the type of the destination array.
- ArgumentException : If the array is multidimensional or the number of elements in the source StringCollection is greater than the available space from index to the end of the destination array.
- The specified array must be of a compatible type.
- This method is an O(n) operation, where n is Count.
Below given are some examples to understand the implementation in a better way:
A B C D E
System.ArgumentOutOfRangeException: Value has to be >= 0.
Parameter name: destinationIndex
- C# | Copy the elements of a string array to the end of the StringCollection
- C# | Copy StringDictionary to Array at the specified index
- C# | Copy ListDictionary to Array instance at the specified index
- C# | Copy OrderedDictionary elements to Array instance at the specified index
- C# | Get or Set at specified index in StringCollection
- C# | Remove from the specified index of the StringCollection
- C# | Index of first occurrence in StringCollection
- C# | Insert at the specified index in StringCollection
- C# | Gets or sets the element at the specified index in StringCollection
- C# | Copy the Stack to an Array
- C# | Copy the entire LinkedList<T> to Array
- C# | How to copy the entire ArrayList to a one-dimensional Array
- Shallow Copy and Deep Copy in C#
- How to create a StringCollection in C#
- C# | Add a string to the end of the StringCollection
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.