In this article, we’re going to talk about the char type, and explore options to convert char array to string in C#.

The char type keyword is an alias for the .NET System.Char structure type that represents a Unicode UTF-16 character.

.NET System.Char array stores string data. Each character can be accessed (and changed) without copying the rest of the characters. That said, it is faster for string manipulation than using String class and it also enables optimizations.

To download the source code for this article, you can visit our GitHub repository.

So, let’s say that we decided to use a char array for fast performance and we need a string object in the end. What are our options?

Creating Char Array

To  convert char array to string in C#, first, we need to create an char[] object:

char[] charArray = { 'c', 'o', 'd', 'e', ' ', 'm', 'a', 'z', 'e' };

Despite we are seeing that there is only 1 character for each member of the array, remember that chars in the C# language are 2 bytes(Unicode UTF-16 character). So for each char, 2 bytes are stored on the stack.

Using String Constructor

The first option to convert char array to string is to use a string constructor:

char[] charArray = { 'c', 'o', 'd', 'e', ' ', 'm', 'a', 'z', 'e' };

var convertedToString = new string(charArray);

Console.WriteLine(convertedToString);

Once we start our application, it is going to print the result in the console window:

code maze

Converting With String.Join() Method

Another option is using the string.Join method:

var joinedToString = string.Join("", charArray);
            
Console.WriteLine(joinedToString);

Again, after we start the application, we are going to see the same result.

Using the String.Concat() Method to Convert Char Array to String

The last option is to use the string.Concat method:

var concatedToString = string.Concat(charArray);

Console.WriteLine(concatedToString); 

Of course, the result is going to be the same as with previous examples.

Conclusion

In this article, we have learned technics on how to convert char[] to string in C#.  It is faster to use a char array for string manipulation and convert char array to string class in the end. 

Array elements are stored together in one block of memory. As a result, allocation and deallocation is not resource-consuming operation.