将元素添加到 List<> 不起作用
Adding elements to List<> doesn't work
我声明了一个List<>,在整个class
中都可以访问
List<article> data;
现在我正在使用一种方法来填充列表<>:
StreamReader sr = new StreamReader(filePath);
while (!sr.EndOfStream)
{
string[] Line = sr.ReadLine().Split(';');
article newArticle = new article();
newArticle.articleNumber = Line[0];
newArticle.description = Line[1];
newArticle.articleId = Line[2];
try
{
data.Add(newArticle);
}
catch(NullReferenceException ex)
{
// Nothing to do here
}
}
每次循环重复,newArticle-Object 包含他的所有元素,因此它肯定不为空。
但它不会添加到数据列表<>。
我错过了什么?
要向列表中添加项目,您必须先对其进行初始化。
替换:
List<article> data;
与:
List<article> data = new List<article>();
我声明了一个List<>,在整个class
中都可以访问List<article> data;
现在我正在使用一种方法来填充列表<>:
StreamReader sr = new StreamReader(filePath);
while (!sr.EndOfStream)
{
string[] Line = sr.ReadLine().Split(';');
article newArticle = new article();
newArticle.articleNumber = Line[0];
newArticle.description = Line[1];
newArticle.articleId = Line[2];
try
{
data.Add(newArticle);
}
catch(NullReferenceException ex)
{
// Nothing to do here
}
}
每次循环重复,newArticle-Object 包含他的所有元素,因此它肯定不为空。 但它不会添加到数据列表<>。 我错过了什么?
要向列表中添加项目,您必须先对其进行初始化。
替换:
List<article> data;
与:
List<article> data = new List<article>();