我正在尝试将2个新数组项添加到现有字符串数组中.我取得了成果,但我确信这不是正确的方法.
如何将项添加到字符串数组中.
string[] sg = {"x", "y" };
string[] newSg = {"z", "w"};
string[] updatedSg = new string[sg.Length+newSg.Length];
for (int i = 0; i < sg.Length; i++)
{
updatedSg[i] = sg[i];
}
for (int i = 0; i < newSg.Length; i++)
{
updatedSg[sg.Length+i] = newSg[i];
}
Run Code Online (Sandbox Code Playgroud)
您无法将项添加到数组中.您可以使用其他容器类型(如List),也可以创建包含更多元素的新数组并复制旧元素.但是您无法向数组添加元素,也无法从数组中删除元素.数组中的元素数是固定的.