C# | Copy StringCollection at the specified index of array Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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. Syntax: public void CopyTo (string[] array, int index); Parameters: 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. Exceptions: 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. Note: 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: Example 1: CSHARP // C# code to copy StringCollection to array, // starting at the specified index of // the target array using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // creating a StringCollection named myCol StringCollection myCol = new StringCollection(); // creating a string array named myArr1 String[] myArr1 = new String[] { "A", "B", "C", "D", "E" }; // Copying the elements of a string // array to the end of the StringCollection. myCol.AddRange(myArr1); // creating a String array named myArr2 String[] myArr2 = new String[myCol.Count]; // Copying StringCollection to array myArr2 // starting from index 0 myCol.CopyTo(myArr2, 0); // Displaying elements in array myArr2 for (int i = 0; i < myArr2.Length; i++) { Console.WriteLine(myArr2[i]); } } } Output: A B C D E Example 2: CSHARP // C# code to copy StringCollection to array, // starting at the specified index of // the target array using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // creating a StringCollection named myCol StringCollection myCol = new StringCollection(); // creating a string array named myArr1 String[] myArr1 = new String[] {"1", "2", "3", "4", "5", "6"}; // Copying the elements of a string // array to the end of the StringCollection. myCol.AddRange(myArr1); // creating a String array named myArr2 String[] myArr2 = new String[myCol.Count]; // Copying StringCollection to array myArr2 // starting from index -1 // This should raise exception "ArgumentOutOfRangeException" // as index is less than 0 myCol.CopyTo(myArr2, -1); // Displaying elements in array myArr2 for (int i = 0; i < myArr2.Length; i++) { Console.WriteLine(myArr2[i]); } } } Output: Unhandled Exception: System.ArgumentOutOfRangeException: Value has to be >= 0. Parameter name: destinationIndex Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.stringcollection.copyto?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Copy StringCollection at the specified index of array S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Collections-Namespace CSharp-Specialized-Namespace CSharp-Specialized-StringCollection +1 More Similar Reads C# | Copy StringDictionary to Array at the specified index StringDictionary.CopyTo(Array, Int32) method is used to copy the string dictionary values to a one-dimensional Array instance at the specified index. Syntax: public virtual void CopyTo (Array array, int index); Parameters: array: It is the one-dimensional Array which is the destination of the values 3 min read C# | Insert at the specified index in StringCollection 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.Insert(Int32, String) method is used to insert a string into the StringCollection 2 min read C# | Remove from the specified index of the StringCollection 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.RemoveAt(Int32) method is used to remove the string at the specified index of the 3 min read C# | Get or Set at specified index in StringCollection 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. Properties: StringCollection accepts null as a valid value and allows duplicate elements. String co 2 min read C# | Copy the elements of a string array to the end of the StringCollection 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.AddRange(String[]) method is used to copy the elements of a string array to the en 2 min read C# | Copy ListDictionary to Array instance at the specified index ListDictionary.CopyTo(Array, Int32) method is used to copy the ListDictionary entries to a one-dimensional Array instance at the specified index. Syntax: public void CopyTo (Array array, int index); Parameters: array : It is the one-dimensional Array which is the destination of the DictionaryEntry o 3 min read C# | Index of first occurrence in StringCollection 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.IndexOf(String) method is used to search the specified string which returns the ze 2 min read C# | Get the number of strings in StringCollection 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.Count property is used to get the number of strings contained in the StringCollect 2 min read C# | Remove all the strings from the StringCollection 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.Clear method is used to remove all the strings from the StringCollection. Syntax: 2 min read C# | Copy the entire LinkedList<T> to Array LinkedList<T>.CopyTo(T[], Int32) method is used to copy the entire LinkedList<T> to a compatible one-dimensional Array, starting at the specified index of the target array. Syntax: public void CopyTo (T[] array, int index); Parameters: array : It is the one-dimensional Array that is the 2 min read Like