C# String TryCopyTo() Method

Intermediate
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, TryCopyTo() copies all characters of a string into a Span<char> (a writable view over some memory, such as an array or a stackalloc buffer). If there is enough room, it copies and returns true. If the destination is too small, it copies nothing and returns false.

The “Try” in the name means no exception for the common failure. The newer CopyTo(Span<char>) overload does the same copy but throws when the space is too small. Both are used in performance-focused code that fills buffers instead of building new strings.

This is an instance member of System.String. Call it on a string variable, such as text.TryCopyTo(...). Calling it on a null variable throws NullReferenceException. It copies characters to a separate place. Strings are immutable, so changing that copy never changes the original string. It is in newer .NET (.NET 6 and later). On older .NET Core, text.AsSpan().TryCopyTo(destination) does the same job.

Syntax

Pass the destination span. Check the returned bool.

C#
Span<char> buffer = stackalloc char[10];

bool ok = "hello".TryCopyTo(buffer);       // True, buffer starts with h e l l o
bool tooBig = "hello world!".TryCopyTo(buffer);   // False, nothing copied
CallWhat it does
text.TryCopyTo(span)Copy if it fits; true or false.
text.TryCopyTo(array)A char[] converts to Span<char> automatically.
text.TryCopyTo(array.AsSpan(3))Copy starting at position 3 of the array.
text.AsSpan().TryCopyTo(span)The same copy on older .NET Core versions.

The whole string is copied to the start of the span. Positions after the copied text keep whatever they held before. To copy part of a string, slice it first: text.AsSpan(2, 3).TryCopyTo(span).

Library declarations
C#
public bool TryCopyTo(Span<char> destination);    // newer .NET
public void CopyTo(Span<char> destination);         // newer .NET, throws if too small

Parameters

ParameterDescription
destinationRequired. The Span<char> that receives the characters. It must be at least Length long.

Return Value

Return ValueDescription
trueAll characters were copied into destination.
falsedestination was too short; nothing was copied.

C# Examples

Example 1 — Copy into a Stack Buffer

Reserve 10 characters on the stack, copy a short word into them, and read the copied part back.

C#
using System;

class Program {
    static void Main() {
        Span<char> buffer = stackalloc char[10];

        bool ok = "hello".TryCopyTo(buffer);
        Console.WriteLine(ok);
        Console.WriteLine(buffer.Slice(0, 5).ToString());
        Console.WriteLine(buffer.Length);
    }
}

How It Works

  1. A temporary buffer. stackalloc char[10] reserves room for 10 characters without creating an object on the heap.
  2. It fits. hello needs 5 places and the buffer has 10, so the call copies and returns True.
  3. Read what was copied. Slice(0, 5) views the first 5 characters, and ToString() on a Span<char> turns them into a string.
  4. The span keeps its size. buffer.Length is still 10; only the first 5 positions were written.

Example 2 — What Happens When It Does Not Fit

Try to copy five characters into three places, and check that the destination was left alone.

C#
using System;

class Program {
    static void Main() {
        char[] box = { '.', '.', '.' };

        bool ok = "hello".TryCopyTo(box);
        Console.WriteLine(ok);
        Console.WriteLine(new string(box));
    }
}

How It Works

  1. An array as the destination. A char[] converts to Span<char> automatically, so you can pass it directly.
  2. Not enough room. Three places cannot hold five characters, so the method returns False.
  3. All or nothing. The dots are still there. TryCopyTo never copies just the part that fits.
  4. No exception. The throwing version, CopyTo(Span<char>), would raise ArgumentException here instead.

Example 3 — Fill a Fixed Buffer Piece by Piece

Copy two words into one array, the second one at an offset, and print the result.

C#
using System;

class Program {
    static void Main() {
        char[] buf = new string('-', 12).ToCharArray();

        bool a = "Hi".TryCopyTo(buf);
        bool b = "Sam".TryCopyTo(buf.AsSpan(3));

        Console.WriteLine(a);
        Console.WriteLine(b);
        Console.WriteLine(new string(buf));
    }
}

How It Works

  1. A pre-filled buffer. new string('-', 12) makes 12 dashes, and ToCharArray() turns them into an editable array.
  2. Copy at the start. Hi overwrites positions 0 and 1.
  3. Copy at an offset. buf.AsSpan(3) is a view that starts at index 3, so Sam lands in positions 3 to 5. Position 2 keeps its dash.
  4. The rest is untouched. The last six positions still hold dashes, and both copies reported True.

Frequently Asked Questions

It copies all characters of the string into a Span<char>. It returns true when they fit and false, copying nothing, when the destination is too short.
CopyTo(Span<char>) throws ArgumentException when the destination is too small. TryCopyTo returns false instead. The older CopyTo(int, char[], int, int) overload copies a range into a char array.
Newer .NET (.NET 6 and later). On earlier .NET Core versions, text.AsSpan().TryCopyTo(destination) gives the same behavior.
Yes. A char[] converts implicitly to Span<char>. Use array.AsSpan(offset) to start copying at a position other than 0.
No. It only reads the string and writes into your buffer. The string itself is immutable.

Next: C# Interview Programs

Practice with hands-on C# interview questions.

Interview programs →

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.

11 people found this page helpful