C# StringBuilder CopyTo() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
StringBuilder

Definition

In C#, StringBuilder.CopyTo(sourceIndex, destination, destinationIndex, count) copies count characters, starting at sourceIndex in the builder, into a char[] starting at destinationIndex. A second overload copies into a Span<char> instead.

It is a way to read characters without creating a string. The builder is not changed, and characters already in the array outside the copied range stay as they were. It mirrors string.CopyTo. When you just need the text as a string, ToString() is simpler.

An instance method of StringBuilder (using System.Text;), called as sb.CopyTo(...). The builder is a mutable buffer that changes in place, unlike an immutable string. It is not thread-safe. The char[] overload exists in every .NET version. The Span<char> overload was added in .NET Core 2.1 and later (also .NET Standard 2.1).

Syntax

Say where to start reading, where to write, and how many characters to copy.

C#
StringBuilder sb = new StringBuilder("Hello");
char[] buffer = new char[3];
sb.CopyTo(1, buffer, 0, 3);          // buffer: e l l

Span<char> span = stackalloc char[2];
sb.CopyTo(3, span, 2);               // span: l o
CallWhat it does
sb.CopyTo(0, arr, 0, sb.Length)Copies the whole text into the start of arr.
sb.CopyTo(2, arr, 5, 3)Copies 3 characters from index 2 into arr[5], arr[6], arr[7].
sb.CopyTo(0, span, 4)Span overload: copies 4 characters into the start of the span.
sb.CopyTo(0, arr, 0, 0)Allowed. Copies nothing.

The overloads check every range. A null array throws ArgumentNullException; negative numbers throw ArgumentOutOfRangeException; and a count that runs past the end of the builder or the destination throws an ArgumentException (or its subclass ArgumentOutOfRangeException). Catching ArgumentException covers both.

Library declarations
C#
public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);
public void CopyTo(int sourceIndex, Span<char> destination, int count); // .NET Core 2.1+

Parameters

ParameterDescription
sourceIndexRequired. The position in the builder of the first character to copy.
destinationRequired. The char[] or Span<char> to write into. It must be big enough.
destinationIndexArray overload only. Where in the array the first copied character goes.
countRequired. How many characters to copy.

Return Value

Return ValueDescription
voidNothing is returned. The characters are written into the destination; the builder is not changed.

C# Examples

Example 1 — Copy One Word Into a char Array

Copy the five letters of World into a new array and show that the builder itself is untouched.

C#
using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder sb = new StringBuilder("Hello, World");
        char[] buffer = new char[5];

        sb.CopyTo(7, buffer, 0, 5);

        Console.WriteLine(new string(buffer));
        Console.WriteLine(buffer.Length);
        Console.WriteLine(sb.ToString());
    }
}

How It Works

  1. An empty array first. new char[5] makes room for exactly the five characters to copy.
  2. Read from index 7. In Hello, World, the W sits at index 7, so five characters from there are World.
  3. Array to text. new string(buffer) turns the filled array into a string just for printing.
  4. Read-only for the builder. Printing sb afterwards still shows Hello, World; copying never removes characters.

Example 2 — Write Into the Middle of an Existing Array

Overwrite four stars in the middle of an array, keep the rest, and catch a copy that asks for more characters than the builder has.

C#
using System;
using System.Text;

class Program {
    static void Main() {
        char[] target = "**********".ToCharArray();
        StringBuilder sb = new StringBuilder("abcdef");

        sb.CopyTo(1, target, 3, 4);
        Console.WriteLine(new string(target));

        try {
            sb.CopyTo(4, target, 0, 5);
        } catch (ArgumentException) {
            Console.WriteLine("Not enough characters to copy");
        }
    }
}

How It Works

  1. A pre-filled array. target starts as ten * characters, so you can see which slots get overwritten.
  2. Source and target positions. CopyTo(1, target, 3, 4) reads bcde from the builder and writes it into slots 3 to 6.
  3. The rest stays. Slots 0 to 2 and 7 to 9 are not touched, so the stars around the letters remain: ***bcde***.
  4. Too many characters. Starting at index 4 leaves only ef, but 5 were requested, so the call throws and the catch block prints a message.

Example 3 — Copy Into a Span Without a New Array

Use the Span<char> overload with a stack buffer for the year, and with an ordinary array for the day.

C#
using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder sb = new StringBuilder("2026-11-18");

        Span<char> year = stackalloc char[4];
        sb.CopyTo(0, year, 4);

        char[] day = new char[2];
        sb.CopyTo(8, day, 2);

        Console.WriteLine(year.ToString());
        Console.WriteLine(new string(day));
        Console.WriteLine(year.Length + day.Length);
    }
}

How It Works

  1. A stack buffer. stackalloc char[4] makes a small Span<char> that needs no heap array at all.
  2. Three arguments. The span overload has no destination index: CopyTo(0, year, 4) fills the span from its start with the first four characters.
  3. Arrays fit too. A char[] converts to Span<char> automatically, so CopyTo(8, day, 2) copies the day 18 using the same overload.
  4. Print the pieces. ToString() on a Span<char> returns its characters as text, giving 2026, and the two buffers hold 4 + 2 = 6 characters.

Frequently Asked Questions

It copies a range of characters from the builder into a char array or a Span<char> that you provide. The builder itself is not changed.
Nothing (void). The result is the characters written into your destination array or span.
CopyTo avoids creating a new string, which helps when you already have a buffer to fill or only need a few characters.
CopyTo(int, Span<char>, int) is in .NET Core 2.1 and later and .NET Standard 2.1. The char[] overload is in every version.
CopyTo throws an ArgumentException (or ArgumentOutOfRangeException) when destinationIndex + count does not fit. It never grows the array for you.

Next: EnsureCapacity()

Reserve room for at least a number of characters before a big build; returns the new capacity.

EnsureCapacity() 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.

12 people found this page helpful