【发布时间】:2012-08-03 11:23:04
【问题描述】:
我有一个问题,我不知道如何在数组长度中创建一个字符串数组变量。
我现在下面有这段代码:
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++
}
感谢解决方案海报
【问题讨论】: