在C#中,可以通过以下语法初始化数组
string[]arr = {"text1","text2"}; // this works
Run Code Online (Sandbox Code Playgroud)
为什么以下不起作用
string[]arr1;
arr1={"phrase1","phrase2"};//Does not compile.
Run Code Online (Sandbox Code Playgroud)
string[] arr = { "text1", "text2" };
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为这是一个特殊的语法,只有在初始化一个数组变量时才允许(个人我甚至不知道它存在).
如果您想稍后为变量分配新数组,则需要说new:
arr = new string[] { "text1", "text2" };
Run Code Online (Sandbox Code Playgroud)
你也可以说new [],编译器会为你找出类型.
| 归档时间: |
|
| 查看次数: |
202 次 |
| 最近记录: |