C# | Copy StringCollection at the specified index of array - GeeksforGeeks

01 Feb, 2019
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:
// C# code to copy StringCollection to array,// starting at the specified index of// the target arrayusing 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:
// C# code to copy StringCollection to array,// starting at the specified index of// the target arrayusing 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
CSharp-Collections-Namespace
CSharp-method
CSharp-Specialized-Namespace
CSharp-Specialized-StringCollection
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Top 50 C# Interview Questions & Answers
Extension Method in C#
HashSet in C# with Examples
Partial Classes in C#
C# | Inheritance
Convert String to Character Array in C#
Linked List Implementation in C#
C# | How to insert an element in an Array?
C# | List Class
Difference between Hashtable and Dictionary in C#