TheDeveloperBlog.com

Home | Contact Us

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

C# Convert Char to String

This C# conversion article converts a char to a string. It provides benchmarks.

Convert char, string. A char can be converted to a one-character string.

There are several ways to do this. The clearest method is the ToString virtual method. But other implementations may have better performance.

ToString

Comparison. The first method here, Method1, uses the string constructor to convert a char to a string. This constructor can repeat a character many times, but we only specify that it be added once.

Method2 simply calls the ToString virtual method on the char type. In the Main entry point, both methods return an equivalent string from the char argument. Their results are equal.

C# program that converts char to string

using System;
using System.Diagnostics;

class Program
{
    static string Method1(char value)
    {
	/*
	 *
	 * Faster way to convert char to string.
	 *
	 * */
	return new string(value, 1);
    }

    static string Method2(char value)
    {
	/*
	 *
	 * Call ToString().
	 *
	 * */
	return value.ToString();
    }

    const int _max = 100000000;
    static void Main()
    {
	Console.WriteLine(Method1('c'));
	Console.WriteLine(Method2('c'));

	var s1 = Stopwatch.StartNew();
	for (int i = 0; i < _max; i++)
	{
	    Method1('c');
	}
	s1.Stop();
	var s2 = Stopwatch.StartNew();
	for (int i = 0; i < _max; i++)
	{
	    Method2('c');
	}
	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"));
	Console.Read();
    }
}

Output

c
c
10.37 ns
10.48 ns

Timing results. On my system, the results consistently indicated that the string constructor was slightly faster. And this held true regardless of what order the methods were tested.

Note: There is probably somewhat less overhead involved in calling the string constructor.

String Constructor

Lookup tables. If your life depended on converting chars to strings quickly, the best thing to do would be to implement a lookup table to do this. The char could be used as an index into a precomputed array of strings.

Tip: This would change a string allocation into an array element load. This improves performance.

Char Lookup Table

Summary. For this particular case, it is probably simplest to just use the ToString method call. In real-world programs, you can sometimes use Substring to take a one-character string from an input string, avoiding the char type.

In many cases: The string constructor has excellent performance and avoids overhead associated with other parts of the .NET Framework.


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