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
Call
What 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
Parameter
Description
destination
Required. The Span<char> that receives the characters. It must be at least Length long.
Return Value
Return Value
Description
true
All characters were copied into destination.
false
destination 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);
}
}
Output
True
hello
10
How It Works
A temporary buffer.stackalloc char[10] reserves room for 10 characters without creating an object on the heap.
It fits.hello needs 5 places and the buffer has 10, so the call copies and returns True.
Read what was copied.Slice(0, 5) views the first 5 characters, and ToString() on a Span<char> turns them into a string.
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));
}
}
Output
False
...
How It Works
An array as the destination. A char[] converts to Span<char> automatically, so you can pass it directly.
Not enough room. Three places cannot hold five characters, so the method returns False.
All or nothing. The dots are still there. TryCopyTo never copies just the part that fits.
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));
}
}
Output
True
True
Hi-Sam------
How It Works
A pre-filled buffer.new string('-', 12) makes 12 dashes, and ToCharArray() turns them into an editable array.
Copy at the start.Hi overwrites positions 0 and 1.
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.
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.