欢迎光临
我们一直在努力

char数组cha

char数组的输出

char[] chars = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'};
Console.WriteLine(chars);//输出: Hello World

Console.WriteLine 的特殊处理

这是由于当输出的是char数组的时候,Console.WriteLine对char[]进行了特殊的重载

Console.WriteLine(new string(message));

与其他类型数组比较

// int 数组 – 输出类型名
int[] intArray = { 1, 2, 3 };
Console.WriteLine(intArray); // 输出:System.Int32[]

// string 数组 – 输出类型名
string[] stringArray = { "A", "B", "C" };
Console.WriteLine(stringArray); // 输出:System.String[]

// char 数组 – 输出内容
char[] charArray = { 'A', 'B', 'C' };
Console.WriteLine(charArray); // 输出:ABC

// bool 数组 – 输出类型名
bool[] boolArray = { true, false };
Console.WriteLine(boolArray); // 输出:System.Boolean[]

char.ToString()

char[] chars = { 'T', 'e', 's', 't' };

Console.WriteLine(chars.ToString()); // 输出:System.Char[]
Console.WriteLine(chars); // 输出:Test
Console.WriteLine(chars.GetType()); // 输出:System.Char[]

// 这是因为:
// 1. chars.ToString() 调用 Object.ToString(),返回类型名
// 2. Console.WriteLine(char[]) 有专门的重载,将数组视为字符串

赞(0)
未经允许不得转载:171主机测评 » char数组cha
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址