Home > Backend Development > C++ > How to Convert a char[] to a String in C#?

How to Convert a char[] to a String in C#?

Linda Hamilton
Release: 2025-01-04 19:08:42
Original
765 people have browsed it

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);
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template