Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# | Check if the specified string is in the StringCollection
Next article icon

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:




// 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:




// 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


Next Article
C# | Check if the specified string is in the StringCollection

S

Sahil_Bansall
Improve
Article Tags :
  • C#
  • CSharp-Collections-Namespace
  • CSharp-method
  • CSharp-Specialized-Namespace
  • CSharp-Specialized-StringCollection

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# | Check if the specified string is in 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.Contains(String) method is used to check whether the specified string is in the St
    2 min read
  • C# | Add a string 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.Add(String) Method is used to add a string to the end of the StringCollection. Syn
    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# | Check if the StringCollection is read-only
    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.IsReadOnly property is used to get a value indicating whether the StringCollection
    1 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences