Copy StringCollection at the specified index of array in C#

The StringCollection.CopyTo() method in C# copies all elements from a StringCollection to a compatible array starting at the specified index. This method is particularly useful when you need to transfer string data from a collection to an array for further processing.

Syntax

Following is the syntax for the CopyTo() method −

public void CopyTo(string[] array, int index)

Parameters

  • array − The one-dimensional array that is the destination of the elements copied from StringCollection.

  • index − The zero-based index in the destination array at which copying begins.

StringCollection.CopyTo() Operation StringCollection [0] "Tim" [1] "Tom" [2] "Sam" [3] "Mark" Target Array [0] null [1] null [2] "Tim" [3] "Tom" [4] null [5] null [6] "Sam" [7] "Mark" CopyTo(arr, 2) Elements copied starting at index 2

Using CopyTo() with Index 0

Example

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main(){
      StringCollection strCol = new StringCollection();
      String[] strArr = new String[] { "Tim", "Tom", "Sam", "Mark", "Katie", "Jacob"};
      
      Console.WriteLine("Original Elements...");
      for (int i = 0; i < strArr.Length; i++) {
         Console.WriteLine(strArr[i]);
      }
      
      strCol.AddRange(strArr);
      String[] arr = new String[strCol.Count];
      strCol.CopyTo(arr, 0);
      
      Console.WriteLine("Elements after copying StringCollection to array:");
      for (int i = 0; i < arr.Length; i++) {
         Console.WriteLine(arr[i]);
      }
   }
}

The output of the above code is −

Original Elements...
Tim
Tom
Sam
Mark
Katie
Jacob
Elements after copying StringCollection to array:
Tim
Tom
Sam
Mark
Katie
Jacob

Using CopyTo() with Specified Index

Example

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main(){
      StringCollection strCol = new StringCollection();
      String[] strArr = new String[] { "Tim", "Tom", "Sam", "Mark", "Katie", "Jacob", "David"};
      
      Console.WriteLine("Original Elements...");
      for (int i = 0; i < strArr.Length; i++) {
         Console.WriteLine(strArr[i]);
      }
      
      strCol.AddRange(strArr);
      String[] arr = new String[10];
      strCol.CopyTo(arr, 3);
      
      Console.WriteLine("Elements after copying starting at index 3:");
      for (int i = 0; i < arr.Length; i++) {
         Console.WriteLine("Index " + i + ": " + (arr[i] ?? "null"));
      }
   }
}

The output of the above code is −

Original Elements...
Tim
Tom
Sam
Mark
Katie
Jacob
David
Elements after copying starting at index 3:
Index 0: null
Index 1: null
Index 2: null
Index 3: Tim
Index 4: Tom
Index 5: Sam
Index 6: Mark
Index 7: Katie
Index 8: Jacob
Index 9: David

Key Rules

  • The destination array must be large enough to accommodate all elements starting from the specified index.

  • The array must be one-dimensional and of type string[].

  • If the index is negative or the array is too small, an exception will be thrown.

  • Elements before the specified index remain unchanged in the destination array.

Conclusion

The StringCollection.CopyTo() method provides an efficient way to copy all collection elements to an array starting at a specified index. This is useful for converting collection data to arrays while maintaining control over the placement within the destination array.

Updated on: 2026-03-17T07:04:36+05:30

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements