可能重复:
如何在我的锯齿状数组中插入一个新数组
我有一个问题,我不知道如何在数组长度中创建一个字符串数组变量。
我现在下面有这段代码:
string[] p = new string[10];
int num = 0;
foreach (Product products in GetAllProducts())
{
//do something
p[num]= "some variable result"
num++
}
问题是,我不知道我会得到多少个“p”,虽然我知道它至少会小于 10。但是如果我把它设置为 0,当我启动它时我会得到一个错误,因为它没有不知道“p[num]”所以我正在寻找某种方法使“p”具有可变长度。
有人可以帮帮我吗?谢谢
============已解决==========
List<string> p = new List<string>();
int num = 0;
foreach (Product products in GetAllProducts())
{
string s= null;
//do something ( create s out of multiple parts += s etc.)
p.add(s)
num++
}
感谢解决方案海报