- 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# - Arithmetic Operators
- C# - Assignment Operators
- C# - Relational Operators
- C# - Logical Operators
- C# - Bitwise Operators
- C# - Miscellaneous Operators
- C# - Operators Precedence
- C# Conditional Statements
- C# - Decision Making
- C# - If
- C# - If Else
- C# - Nested If
- C# - Switch
- C# - Nested Switch
- C# - Switch Expressions
- C# Control Statements
- C# - Loops
- C# - For Loop
- C# - While Loop
- C# - Do While Loop
- C# - Nested Loops
- C# - Break
- C# - Continue
- C# - Foreach Loop
- C# - Goto Statement
- C# OOP & Data Handling
- 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# - LINQ
- C# - IEnumerable vs IEnumerator
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Tasks and Parallel Programming
- C# - Multithreading
- C# - Extension Methods
- C# - Async and Await
- C# Modern Features
- C# - Tuples
- C# - Pattern Matching Enhancements
- C# - Nullable Reference Types
- C# - What's New in C# 11 / 12 / 13
- C# Practical & Advanced Usage
- C# - JSON & XML Handling
C# Array - CopyTo() Method
The C# Array CopyTo() method copies all elements of the current one-dimensional array to another one-dimensional array starting at the specified index in the destination array.
According to the overloaded CopyTo method, the integer bit varies; You can pass a 32-bit integer index or a 64-bit integer index.
Exception
There are the following exceptions of CopyTo() methods −
- ArgumentNullException: Thrown if the destination array is null.
- ArgumentOutOfRangeException: Thrown if the index is less than zero.
- ArgumentException Thrown if the destination array does not have enough space to accommodate the copied element or is of an incompatible type.
Syntax
Following is the syntax of the C# Array CopyTo() method −
public void CopyTo(Array array, int index);
Parameters
This method accepts the following parameters −
- array: The destination array where the element will be copied.
- index: The index value in the destination array at which copying begins.
Return value
This method does not return any value.
Example 1: Copy an Array of Int
This is the basic example of theCopyTo()method, Here we copy an array of integers −
using System;
class Program {
static void Main() {
int[] source = { 1, 2, 3, 4, 5 };
int[] destination = new int[10];
source.CopyTo(destination, 2);
Console.WriteLine(string.Join(", ", destination));
}
}
Output
Following is the output −
0, 0, 1, 2, 3, 4, 5, 0, 0, 0
Example 2: Copy String into Another Array
Let us see another example of the CopyTo() method. Here, we copy an array of strings into another with a starting index 3 −
using System;
class Program {
static void Main() {
string[] source = { "apple", "banana", "cherry" };
string[] destination = new string[5];
source.CopyTo(destination, 1);
Console.WriteLine(string.Join(", ", destination));
}
}
Output
Following is the output −
, apple, banana, cherry,
Example 3: Copy Characters to Another Array
In the previous example we have copied integer and string array, here we use the CopyTo() method to copy the character array −
using System;
class Program {
static void Main() {
char[] source = { 'A', 'B', 'C' };
char[] destination = new char[6];
source.CopyTo(destination, 3);
Console.WriteLine(string.Join(", ", destination));
}
}
Output
Following is the output −
., ., ., A, B, C
Example 4: Handling Exception
This is another example of the CopyTo() method. Here, we are handling the exception and displaying the exception −
using System;
class Program {
static void Main() {
try {
int[] source = { 1, 2, 3 };
int[] destination = new int[2];
source.CopyTo(destination, 0);
}
catch (Exception ex) {
Console.WriteLine($"Exception: {ex.Message}");
}
}
}
Output
Following is the output −
Exception: Destination array was not long enough. Check destIndex and length, and the array's lower bounds Parameter name: destinationArray