可能重复:
如何将新数组插入到我的锯齿状数组中
我有一个问题,我不知道如何在数组长度中创建一个字符串数组变量.
我现在有以下代码:
string[] p = new string[10];
int num = 0;
foreach (Product products in GetAllProducts())
{
//do something
p[num]= "some variable result"
num++
}
Run Code Online (Sandbox Code Playgroud)
问题是,我不知道有多少"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++
}
Run Code Online (Sandbox Code Playgroud)
thanx到解决方案海报