Home >Backend Development >C++ >How Do I Efficiently Convert a char[] to a string in C#?

How Do I Efficiently Convert a char[] to a string in C#?

Barbara Streisand
Barbara StreisandOriginal
2025-01-04 08:49:35782browse

How Do I Efficiently Convert a char[] to a string in C#?

Converting Character Array to String in .NET / C#

Determining the optimal method to convert a char[] to a string is a common challenge in C# programming. Unfortunately, the ToString() method proves ineffective in this scenario.

However, the solution lies in utilizing the constructor designed specifically for this task:

char[] chars = {'a', ' ', 's', 't', 'r', 'i', 'n', 'g'};
string s = new string(chars);

By instantiating a new string object using this constructor, you can effortlessly convert the character array into the desired string.

The above is the detailed content of How Do I Efficiently Convert a char[] to a string in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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