How to Convert Char Array to String in C#.NET? [5 Methods]

In this C#.Net tutorial, I will explain, how to convert char array to a string in C#.Net.

To convert a char array to a string in C#.Net, use one of the following 5 methods:

  • Using the string Constructor
  • Using string.Join()
  • Using StringBuilder
  • Using string.Concat()
  • Using LINQ’s Aggregate Method

Convert Char Array to String in C#.NET

Let us check the 5 different methods to convert a char array to a string in C#.Net.

Using the string Constructor

The string class in C# has a constructor that takes a char array as an argument and returns a new string that represents the characters in the array.

Example:

using System;

namespace CharArrayToStringExample
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] charArray = {'H', 'e', 'l', 'l', 'o'};
            string str = new string(charArray);
            Console.WriteLine(str);  // Output: Hello
        }
    }
}

Once you run the code using Visual Studio, you can see the output below:

Convert Char Array to String in C#.NET

Using string.Join()

The string.Join() method can be used to join elements of an array into a string in C#. Even though this method is generally used to join strings, it can also work on char arrays if you specify an empty separator.

Example:

using System;

namespace CharArrayToStringExample
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] charArray = {'W', 'o', 'r', 'l', 'd'};
            string str = string.Join("", charArray);
            Console.WriteLine(str);  // Output: World
        }
    }
}

You can see the output like below:

Convert Char Array to String in C#.NET

Using StringBuilder

StringBuilder is a mutable sequence of characters, and it’s a part of the System.Text namespace. It can be used to convert a char array to a string by appending each character to the StringBuilder object and then converting it to a string.

Example:

using System;
using System.Text;

namespace CharArrayToStringExample
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] charArray = {'!', ' ', 'H', 'i'};
            StringBuilder sb = new StringBuilder();
            foreach (char ch in charArray)
            {
                sb.Append(ch);
            }
            string str = sb.ToString();
            Console.WriteLine(str);  // Output: ! Hi
        }
    }
}

Using string.Concat()

The string.Concat() method concatenates the string representations of two or more objects. It can also be used to convert a char array to a string.

Example:

using System;

namespace CharArrayToStringExample
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] charArray = {'C', '#', '.', 'N', 'E', 'T'};
            string str = string.Concat(charArray);
            Console.WriteLine(str);  // Output: C#.NET
        }
    }
}

Using LINQ’s Aggregate Method

LINQ offers an Aggregate method that can be used to convert a char array to a string as well. Here’s how to do it:

Example:

using System;
using System.Linq;

namespace CharArrayToStringExample
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] charArray = {'E', 'x', 'a', 'm', 'p', 'l', 'e'};
            string str = charArray.Aggregate("", (current, next) => current + next);
            Console.WriteLine(str);  // Output: Example
        }
    }
}

Performance Considerations

When choosing a method for conversion, it’s essential to consider the performance implications:

  • Using the string constructor is generally the most efficient way to convert a char array to a string.
  • StringBuilder is more efficient for large arrays or when you need to perform multiple string manipulations.
  • string.Join() and string.Concat() are convenient but may not be as efficient for large arrays.
  • Using LINQ’s Aggregate method is the least efficient of all these methods.

Conclusion

In this C#.Net tutorial, I have shown you with practical example of how to convert a char array to a string in C#.NET using various methods.

You may also like: