C#运用List动态添加元素

C#中的数组是不支持动态添加元素的,只能创建固定大小的数组。

解决方法:可以使用List

List<string> result = new List<string>();
for (int i = 0; i < 5; i++ )
{
    result.Add(i.ToString());
}

之后也可以转为数组类型:

 string[] strArray = result.ToArray();

附:遍历List中的元素

foreach (T element in mList) 
{
     Console.WriteLine(element);
}


解决方法:可以使用List

猜你喜欢

转载自blog.csdn.net/bocksong/article/details/80916714
今日推荐