如何在c#中创建一维动态数组?

Sas*_*ska 3 c# arrays dynamic

关于c#的noob问题:如何创建一维动态数组?以及如何改变它?

谢谢.

Thi*_*ise 14

您可以List<>在C#中使用该对象,而不是使用数组.

List<int> integerList = new List<int>();
Run Code Online (Sandbox Code Playgroud)

要迭代列表中包含的项目,请使用foreach运算符:

foreach(int i in integerList)
{
    // do stuff with i
}
Run Code Online (Sandbox Code Playgroud)

您可以使用Add()Remove()功能在列表对象中添加项目.

for(int i = 0; i < 10; i++)
{
    integerList.Add(i);
}

integerList.Remove(6);
integerList.Remove(7);
Run Code Online (Sandbox Code Playgroud)

您可以List<T>使用以下ToArray()函数将a转换为数组:

int[] integerArray = integerList.ToArray();
Run Code Online (Sandbox Code Playgroud)

这是关于对象的文档List<>.