ToCharArray. This C# method converts strings to character arrays. It is called on a string and returns a new char array. The original string is left unchanged.
It is possible to manipulate a char array in-place, which we cannot do with a string. This makes many performance optimizations possible.
Example. First we use ToCharArray() to get a character array from the contents of a string. The example uses an input string, and then assigns a char array to the result of ToCharArray.
Next It uses a for-loop to get each character in the array, finally writing it to the Console.
using System;
// Input string.
string value = "abcd";
// Use ToCharArray to convert string to array.
char[] array = value.ToCharArray();
// Loop through array.
for (int i = 0; i < array.Length; i++)
{
// Get character from array.
char letter = array[i];
// Display each letter.
Console.Write("Letter: ");
Console.WriteLine(letter);
}Letter: a
Letter: b
Letter: c
Letter: d
Benchmark, ToCharArray. Here we measure the performance of ToCharArray. We copy a string to a char array with 2 versions of C# code.
Version 1 This uses ToCharArray. We make sure the resulting string has a correct first character each time.
Version 2 We allocate a char array of the required length, and then copy each character in from the source string.
Result ToCharArray is faster on a 300-char string. But if we try a smaller string, ToCharArray may be slower—make sure to test.
using System;
using System.Diagnostics;
const int _max = 2000000;
// Create 500-char string.
string text = new string('P', 500);
// Version 1: use ToCharArray.
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
char[] result = text.ToCharArray();
if (result[0] != 'P')
{
return;
}
}
s1.Stop();
// Version 2: copy chars from string with loop.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
char[] result = new char[text.Length];
for (int v = 0; v < text.Length; v++)
{
result[v] = text[v];
}
if (result[0] != 'P')
{
return;
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); 68.30 ns ToCharArray
323.36 ns new char[], for loop
Uses. With ToCharArray, the char array you receive is mutable. You can change characters. The method is ideal for transforming chars (like in ROT13, or uppercasing the first letter).
ToCharArray is often useful: it returns a character array filled with the string's characters. We can modify this array in-place—this improves the performance of code.
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.