C# 程序将字符列表转换为字符串
csharpserver side programmingprogramming更新于 2025/9/24 13:10:00
首先,声明字符数组并设置每个字符的值 −
char[] ch = new char[5]; ch[0] = 'H'; ch[1] = 'e'; ch[2] = 'l'; ch[3] = 'l'; ch[4] = 'o';
现在,使用字符串类构造函数并从上述字符数组创建一个新字符串 −
string myChar = new string(ch);
示例
让我们看看在 C# 中将字符列表转换为字符串的代码。
using System;
namespace Demo {
class MyApplication {
static void Main(string[] args) {
char[] ch = new char[5];
ch[0] = 'H';
ch[1] = 'e';
ch[2] = 'l';
ch[3] = 'l';
ch[4] = 'o';
string myChar = new string(ch);
Console.WriteLine("Converted to string = {0}", myChar);
}
}
}
输出
Converted to string = Hello

