Home
C#
char Array
This page was last reviewed on Sep 21, 2023.
Dot Net Perls
Char array. A C# char array stores string data. Each character can be accessed (and changed) without copying the rest of the characters. It enables optimizations.
Shows a char array
A char array is an alternative to using StringBuilder for quickly creating string data. We evaluate StringBuilder and compare it to character arrays.
StringBuilder
Array
Example. First we create new char arrays. When you use the new operator, the execution engine allocates memory on the managed heap.
new
Note Array elements are stored together in one block of memory. This reduces the overhead with storing separate objects.
object
And Char is a value type so the value itself, not a reference, is stored in the storage location.
char
Shows a char array
using System; char[] array1 = { 'a', 'b', 'c' }; char[] array2 = new char[] { 'a', 'b', 'c' }; char[] array3 = new char[3]; array3[0] = 'a'; array3[1] = 'b'; array3[2] = 'c'; // Write total length. Console.WriteLine("LENGTH: {0}", array1.Length + array2.Length + array3.Length);
LENGTH: 9
Benchmark, char array. Character arrays can be used to store character data such as letters or numbers. Here we append characters one-by-one to an array using the char data type.
Note Remember that chars in the C# language are 2 bytes. So for each char, 2 bytes are copied on the stack.
Version 1 This version uses a char array. We assign each index to the char we want to append. We use the string constructor.
String Constructor
Version 2 We use StringBuilder here. With StringBuilder, you could keep appending items to the buffer after the 100th char.
Result With .NET 5 on Linux (in 2021) this benchmark still shows that using a char array is much faster than the StringBuilder.
using System; using System.Diagnostics; using System.Text; const int _max = 1000000; var s1 = Stopwatch.StartNew(); // Version 1: use a new char array. for (int i = 0; i < _max; i++) { char[] buffer = new char[100]; for (int v = 0; v < 100; v++) { buffer[v] = 'a'; } string result = new string(buffer); } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: use a StringBuilder. for (int i = 0; i < _max; i++) { StringBuilder builder = new StringBuilder(100); for (int v = 0; v < 100; v++) { builder.Append('a'); } string result = builder.ToString(); } 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"));
86.30 ns char array 288.69 ns StringBuilder
Notes, performance. Char arrays are faster because they have fewer features. Meanwhile StringBuilder handles many cases and has many methods.
And StringBuilder is easy to use and a great optimization, but it is far slower in simple cases. It can help in real programs.
Note We used char arrays as an improvement in the alphanumeric sorting algorithm. This improved performance.
Sort Alphanumeric
Result Char arrays can be around 7 times faster on certain tasks. Char buffers have a big performance advantage.
A discussion. Using char arrays allows us to use more lower-level algorithms with greater performance. The char array code forces us to have more "coding discipline."
And It is more exact if you can enforce the constraints of char arrays (like the fixed length).
Array Length
Note StringBuilder could have extra chars appended, but the char array wouldn't allow that.
Detail The IndexOutOfRangeException that would be thrown is useful for debugging the algorithm.
IndexOutOfRangeException
Summary. Using char arrays is better when you know in advance the number of characters. StringBuilder is fast and useful, but using char arrays is sometimes more appropriate.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 21, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.