TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Char Combine Method

This C# program tests the performance of combining multiple chars into strings.

Char combine. Several chars can be concatenated or combined into a string.

We test whether the string.Concat method is useful. Is there is a better alternative? We look at a way you can combine several characters.

string.Concat

Combine chars benchmarks

CharCombine method:  520 ms
Char concat:        1887 ms

Example. Here we see an optimization that deals with char arrays and the new string constructor. My other work shows that when you need to build a string char-by-char, you should use a char array for best performance.

Char Array

C# program that combines four chars

using System;

class CharTool
{
    /// <summary>
    /// Combine four chars.
    /// </summary>
    public static string CharCombine(char c0, char c1, char c2, char c3)
    {
	// Combine chars into array
	char[] arr = new char[4];
	arr[0] = c0;
	arr[1] = c1;
	arr[2] = c2;
	arr[3] = c3;
	// Return new string key
	return new string(arr);
    }
}

class Program
{
    static void Main()
    {
	char a = 'a';
	char b = 'b';
	char c = 'c';
	char d = 'd';
	char e = 'e';
	// Use CharCombine method
	Console.WriteLine(CharTool.CharCombine(a, b, c, d));
	Console.WriteLine(CharTool.CharCombine(b, c, d, e));
    }
}

Output

abcd
bcde

This method receives four chars, which are two bytes each in the C# language. For calling it, you need to pass it four chars. These char values would be dynamic in a real program.

Tip: It is contained in a static class and is a static method, which is ideal for a utility method such as this. It does not save state.

Static Method, Class and Constructor

In the method body, it allocates a new char array with four elements. Then, it assigns slots at indices 0 to 3 to the four parameter chars. This is how it creates the buffer, which it uses in the return statement.

And: The new string(char[]) constructor is invoked in the return statement, which builds the string from the char buffer we created.

String ConstructorReturn Statement

Method result. The final result of the method is that the four char parameters are combined, joined into a single string. You can easily use this string in the rest of your program.

Benchmark. The alternative here is the string.Concat method, which you can use with "+" between the four chars with ToString(). It is much slower and does unnecessary things. Performance figures are shown above.

Varibles used in benchmarks: C#

char a = 'a';
char b = 'b';
char c = 'c';
char d = 'd';

Code benchmarked in loops: C#
    10000000 iterations benchmarked.

// Custom method
string s1 = CharTool.CharCombine(a, b, c, d); // [above]
string s2 = CharTool.CharCombine(d, c, b, a);

// Concatenation
string s1 = a.ToString() + b.ToString() + c.ToString() + d.ToString();
string s2 = d.ToString() + c.ToString() + b.ToString() + a.ToString();

Summary. We allocated a char array as a buffer to store four chars. You can easily adapt the method to handle two, three, five, or even more chars, and it will likely perform well. We should be careful with the params keyword.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf