
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Copy StringCollection at the specified index of array in C#
To copy StringCollection at the specified index of the array, the code is as follows −
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("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]); } } }
Output
This will produce the following output −
Elements... Tim Tom Sam Mark Katie Jacob Elements...after copying StringCollection to array Tim Tom Sam Mark Katie Jacob
Example
Let us now see another 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("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"); for (int i = 0; i < arr.Length; i++) { Console.WriteLine(arr[i]); } } }
Output
This will produce the following output −
Elements... Tim Tom Sam Mark Katie Jacob David Elements...after copying Tim Tom Sam Mark Katie Jacob David
- Related Articles
- Insert at the specified index in StringCollection in C#
- Copy StringDictionary to Array at the specified index in C#
- Copy ListDictionary to Array instance at the specified index in C#
- Get or Set at specified index in StringCollection in C#
- Copy OrderedDictionary elements to Array instance at the specified index in C#
- Gets or sets the element at the specified index in StringCollection in C#
- Remove from the specified index of the StringCollection in C#
- Golang Program to insert an element into the array at the specified index
- Remove element at specified index of Collection in C#
- Index of first occurrence in StringCollection in C#?
- Copying the entire ArrayList to 1-D Array starting at the specified index in C#
- Remove the element at the specified index of the ArrayList in C#
- Java Program to copy an array from the specified source array
- Remove the entry at specified index from OrderedDictionary in C#
- Replace an element at a specified index of the Vector in Java

Advertisements