Home >Backend Development >C++ >How to Convert a char[] to a String in C#?
Conversion from char[] to String in .NET/C#
Converting an array of characters (char[]) to a string can be achieved using the dedicated constructor in C#. The ToString() method mentioned in the question is not applicable in this scenario.
To illustrate:
char[] chars = {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'}; string s = new string(chars);
In this example, a char[] named "chars" is initialized with several characters. The new string() constructor is then employed to create a string "s" from the char[]. This method takes a char[] as an argument and constructs a string containing all the characters from the array.
Consequently, the string "s" will now hold the value "a string."
While the ToString() method can be used to convert various types to strings, such as objects and values, it does not perform the specific conversion from char[] to string. Instead, the new string() constructor provides an efficient way to accomplish this task in C#.
The above is the detailed content of How to Convert a char[] to a String in C#?. For more information, please follow other related articles on the PHP Chinese website!