C# Char Array

Use char arrays to store character and string data. Char arrays can replace StringBuilder usage.
Char array. A char array stores string data. Each character can be accessed (and changed) without copying the rest of the characters. It enables optimizations.
Notes, StringBuilder. A char array is an alternative to using StringBuilder for quickly creating string data. We evaluate StringBuilder and compare it to character arrays.StringBuilder
Example. First we create new char arrays. When you use the new operator, the execution engine in the Common Language Runtime 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.


And: Char is a value type so the value itself, not a reference, is stored in the storage location.

C# program that declares char arrays using System; class Program { static void Main() { char[] array1 = { 's', 'a', 'm' }; char[] array2 = new char[] { 's', 'a', 'm' }; char[] array3 = new char[3]; array3[0] = 's'; array3[1] = 'a'; array3[2] = 'm'; // Write total length: Console.WriteLine(array1.Length + array2.Length + array3.Length); } } Output 9
Char array, StringBuilder. 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.

Next: Here is some example code where we replace StringBuilder with a char array. This often improves performance.

Note: Remember that chars in the C# language are 2 bytes. So for each char, 2 bytes are copied on the stack.

Program 1: The first program uses a char array. We assign each index to the char we want to append. It uses the string constructor.

String Constructor

Program 2: We use StringBuilder here. With StringBuilder, you could keep appending items to the buffer after the 100th char.

C# program that uses char array, fast class Program { static void Main() { // Use a new char array. char[] buffer = new char[100]; for (int i = 0; i < 100; i++) { buffer[i] = 'a'; } string result = new string(buffer); } } C# program that uses StringBuilder, slow using System.Text; class Program { static void Main() { // Declare new StringBuilder and append to it 100 times. StringBuilder builder = new StringBuilder(100); for (int i = 0; i < 100; i++) { builder.Append('a'); } string result = builder.ToString(); } } Output Char[] buffer: 255 ms [faster] StringBuilder: 1804 ms
Benchmark. Char arrays are faster because they don't have as many features. StringBuilder handles many cases and has many methods.

StringBuilder: This 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.

Alphanumeric Sort

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.

Therefore: The IndexOutOfRangeException that would be thrown is useful for debugging the algorithm.

A 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.
Summary, continued. An array may also encourage higher-quality code over a StringBuilder. But sometimes the excess work to use a char array is not worth the trouble.Array
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls