Example. Here we see a method that deals with char arrays and the new string constructor. When you need to build a string char-by-char, you should use a char array for best performance.
using System;
class Program
{
public static string CharCombine(char c0, char c1, char c2, char c3)
{
// Allocate an array, and assign its elements to the arguments, then convert to a string.
char[] array = new char[4];
array[0] = c0;
array[1] = c1;
array[2] = c2;
array[3] = c3;
return new string(array);
}
static void Main()
{
char a = 'a';
char b = 'b';
char c = 'c';
char d = 'd';
char e = 'e';
// Use CharCombine method.
Console.WriteLine(CharCombine(a, b, c, d));
Console.WriteLine(CharCombine(b, c, d, e));
}
}abcd
bcde
Benchmark. What is the fastest way to convert multiple chars to a string? This benchmark will help us learn the answer to this question. The CharCombine() method is faster.
Version 1 Here we call CharCombine() with 4 arguments in a tight loop. We ensure the returned strings are valid.
Version 2 The alternative here is the string.Concat method, which you can use with "+" between the chars with ToString().
Result In .NET 8, using a char array to combine multiple chars into a string is significantly faster.
using System;
using System.Diagnostics;
class Program
{
public static string CharCombine(char c0, char c1, char c2, char c3)
{
char[] array = new char[4];
array[0] = c0;
array[1] = c1;
array[2] = c2;
array[3] = c3;
return new string(array);
}
public static void Main()
{
char a = 'a';
char b = 'b';
char c = 'c';
char d = 'd';
const int _max = 1000000;
var s1 = Stopwatch.StartNew();
// Version 1: combine chars with char array.
for (int i = 0; i < _max; i++)
{
string v1 = CharCombine(a, b, c, d);
string v2 = CharCombine(d, c, b, a);
if (v1 != "abcd" || v2 != "dcba") {
return;
}
}
s1.Stop();
var s2 = Stopwatch.StartNew();
// Version 2: use ToString and concatenation.
for (int i = 0; i < _max; i++)
{
string v1 = a.ToString() + b.ToString() + c.ToString() + d.ToString();
string v2 = d.ToString() + c.ToString() + b.ToString() + a.ToString();
if (v1 != "abcd" || v2 != "dcba") {
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"));
}
}22.27 ns CharCombine
60.78 ns +
Summary. We allocated a char array as a buffer to store 4 chars. You can easily adapt the method to handle two, three, five, or even more chars, and it will likely perform well.
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 Dec 3, 2023 (new example).