Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
所以我初始化了这个数组:
string[] names = { "Joe", "Bob", "", "", "Marcus", "" };
现在我想以类似的方式更改内容。我试过了:
names = {"Happy", "", "", "Go", "", "Lucky"};
但这会导致错误,
只有赋值、调用、递增、递减和新对象表达式可以用作语句
有没有办法像我试图做的那样批量分配数组的内容?
原始{ ... }语法只能在声明数组时使用。
{ ... }
要创建一个新的数组实例,请使用new string[] { ... }. 然后,您可以将此新数组实例分配给变量:
new string[] { ... }
names = new string[] {"Happy", "", "", "Go", "", "Lucky"};
请注意,旧的数组实例不会受到影响。