为什么以下无法编译?

Ada*_*Lee 1 c# c#-4.0

String s = new String({'h','e','l','l','o'});
Run Code Online (Sandbox Code Playgroud)

我收到了错误:

1无效的表达式术语'{'

我以为{'h','e','l','l','o'}应该是一个字符数组,为什么它无法编译?

And*_*ker 6

我想你的意思是:

String s = new String(new[] {'h','e','l','l','o'});
Run Code Online (Sandbox Code Playgroud)

您以前的代码没有正确初始化数组.有关更多信息,请查看有关隐式类型数组的MSDN文章.

您还可以显式指定数组的类型:

String s = new String(new char[] {'h','e','l','l','o'});
Run Code Online (Sandbox Code Playgroud)