C# String CopyTo() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

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.

C#
text.CopyTo(0, buffer, 0, 3);
CallWhat 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
C#
public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);

Parameters

ParameterDescription
sourceIndexRequired. Where to start reading in the string.
destinationRequired. The char array that receives the characters.
destinationIndexRequired. Where to start writing in the array.
countRequired. How many characters to copy.

Return Value

Return ValueDescription
voidNothing 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.

C#
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)}");
    }
}

How It Works

  1. Size the buffer. new char[text.Length] holds every character.
  2. Copy from the start. CopyTo(0, buffer, 0, text.Length) reads and writes from index 0.
  3. Print the array. Console.WriteLine(buffer) prints the characters as text.
  4. 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.

C#
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

  1. Start with a filled buffer. Seven dashes give visible placeholders.
  2. Pick sourceIndex 4. From "CodeToFun", that begins at 'T'.
  3. Write at destinationIndex 2. Four chars overwrite positions 2–5 → ToFu.
  4. 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.

C#
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)}");
    }
}

How It Works

  1. Copy into a mutable buffer. CopyTo fills buffer with h-e-l-l-o.
  2. Change one character. Uppercase index 0 → 'H'.
  3. Original stays the same. The string text is still "hello".
  4. 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.

Next: EndsWith()

Learn how to check whether a string ends with a suffix.

EndsWith() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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