C# String CopyTo() Method

Definition
In C#, CopyTo() copies characters from a string into a char[]. You choose where to start in the string and where to start in the array.
You pass the start index, the destination array, the array start index, and the count. All four are required. There is no return value.
Syntax
Pass source index, destination array, destination index, and count.
text.CopyTo(0, buffer, 0, 3);
| Call | What it does |
|---|
text.CopyTo(0, buffer, 0, 3) | Copies 3 characters from the start of text into buffer. |
Prefer ToCharArray() when you want a full copy of every character.
Library declarations
public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);
Parameters
| Parameter | Description |
|---|
| sourceIndex | Required. Where to start reading in the string. |
| destination | Required. The char array that receives the characters. |
| destinationIndex | Required. Where to start writing in the array. |
| count | Required. How many characters to copy. |
Return Value
| Return Value | Description |
|---|
| void | Nothing is returned. The array is updated in place. |
C# Examples
Example 1 — Copy All Characters
Allocate a buffer the same length as the string, then copy every character starting at index 0.
using System;
class Program {
static void Main() {
string text = "C# rocks";
char[] buffer = new char[text.Length];
text.CopyTo(0, buffer, 0, text.Length);
Console.WriteLine(buffer);
Console.WriteLine($"As string: {new string(buffer)}");
}
}
C# rocks
As string: C# rocks
How It Works
- Size the buffer.
new char[text.Length] holds every character. - Copy from the start.
CopyTo(0, buffer, 0, text.Length) reads and writes from index 0. - Print the array.
Console.WriteLine(buffer) prints the characters as text. - Rebuild if needed.
new string(buffer) creates a string from the array.
Example 2 — Partial Copy with Offsets
Copy only a slice of the string, and write it at a non-zero index in a larger buffer.
using System;
class Program {
static void Main() {
string text = "CodeToFun";
char[] buffer = { '-', '-', '-', '-', '-', '-', '-' };
// Copy "ToFu" (indexes 4..7) into buffer starting at index 2
text.CopyTo(4, buffer, 2, 4);
Console.WriteLine(new string(buffer));
}
}
How It Works
- Start with a filled buffer. Seven dashes give visible placeholders.
- Pick sourceIndex 4. From
"CodeToFun", that begins at 'T'. - Write at destinationIndex 2. Four chars overwrite positions 2–5 →
ToFu. - Edges stay. Indexes 0, 1, and 6 remain
'-'.
Example 3 — Edit Chars, Then Rebuild
Strings are immutable, but the destination array is not—edit characters after copying, then build a new string.
using System;
class Program {
static void Main() {
string text = "hello";
char[] buffer = new char[text.Length];
text.CopyTo(0, buffer, 0, text.Length);
buffer[0] = char.ToUpper(buffer[0]);
Console.WriteLine($"Original: {text}");
Console.WriteLine($"Edited: {new string(buffer)}");
}
}
Original: hello
Edited: Hello
How It Works
- Copy into a mutable buffer.
CopyTo fills buffer with h-e-l-l-o. - Change one character. Uppercase index 0 →
'H'. - Original stays the same. The string
text is still "hello". - Build a new string.
new string(buffer) yields "Hello".
Frequently Asked Questions
CopyTo() copies characters from a string into a char array.
All four are required: sourceIndex, destination, destinationIndex, and count.
Nothing. It is void and updates the destination array.
An exception is thrown when the range does not fit.
Use ToCharArray when you want every character in a new array.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful