Home
C#
String CopyTo Method
Updated Nov 8, 2023
Dot Net Perls
CopyTo. This C# method takes string characters and puts them into an array. It copies a group of characters from one source string into a character array.
string.Copy
Array
Optimized code. This .NET string method provides optimized low-level code. When we need to copy chars from a string, CopyTo is a good solution.
An example. CopyTo() must be called on an instance of a string object. String objects in C# can be represented by string literals. So we can call CopyTo on a literal as well.
String Literal
Important The char array variable is allocated before CopyTo is invoked. This provides the required memory storage.
char Array
Detail CopyTo() is void, and returns nothing. The char array in the program instead has its characters changed internally in CopyTo.
Note Arrays are indexed by their offsets—so the index 1 is the second character "b."
Console.WriteLine
using System; string value1 = "abcd"; char[] array1 = new char[3]; // Copy the final 3 characters to the array. value1.CopyTo(1, array1, 0, 3); // Print the array we copied to. Console.WriteLine($"RESULT LENGTH = {array1.Length}"); Console.WriteLine(array1);
RESULT LENGTH = 3 bcd
A benchmark. I wanted to know whether using CopyTo is faster than a for-loop on a short string. Should we use CopyTo in performance-critical methods?
Version 1 Copy chars from a string into a char array with CopyTo. Only 10 chars are copied.
Version 2 Copy those same 10 chars into the char array with a for-loop. Use an index expression to assign elements.
Result When tested in separate runs of the program on .NET 5 for Linux (in 2021), I find string CopyTo is faster.
Also CopyTo begins becoming much faster as the source string becomes longer. But even at 10 chars it is faster overall.
using System; using System.Diagnostics; const int _max = 100000000; char[] values = new char[100]; string temp = "0123456789"; // Version 1: use CopyTo. // Version 2: use for-loop. var s1 = Stopwatch.StartNew(); if (true) { for (int i = 0; i < _max; i++) { temp.CopyTo(0, values, 0, temp.Length); } } else { for (int i = 0; i < _max; i++) { for (int j = 0; j < temp.Length; j++) { values[j] = temp[j]; } } } s1.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));
7.38 ns, CopyTo 12.39 ns, For-loop
Substring. In most .NET programs, the CopyTo method is not necessary. Instead, your programs will often use Substring to copy one range of characters to another.
String Substring
But CopyTo() along with ToCharArray can be used as optimizations. And CopyTo can help when char arrays are needed by other methods.
String ToCharArray
Summary. CopyTo() allows you to copy ranges of characters from a source string into target arrays. It is often not needed, but can replace a for-loop to copy chars.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Nov 8, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen