我在收藏清单方面遇到了一些问题.我使用该列表来保存我从xml和textfiles收集的一些客户数据.
// First i create an instance of the list
List<Customer> cusList = new List<Customer>();
// Save files
String[] somefiles = Directory.GetFiles(//FromPath");
// Then i loop through some files and collect data for the list
for (int i=0; i<somefiles.length; i++)
{
if (some statements match)
{
// call a methode and save file data to the cuslist
cuslist= callmethode(somefiles);
}
else
{
System.Console.WriteLine("Do nothing");
}
}
Run Code Online (Sandbox Code Playgroud)
我想扩展所有文件的列表,但目前我只得到循环数据来自最后一个文件.
我如何处理它,它保存所有文件中的所有数据?
亲切的问候
在您编写时,cuslist= callmethode(file);您会在每次迭代时重新分配列表.你需要的是这样的:
cuslist.AddRange(callmethode(file));
Run Code Online (Sandbox Code Playgroud)
这只会将方法返回的元素添加到列表中,而不是替换整个列表.
如果该方法只返回一个单独的元素使用cuslist.Add.