我正在尝试读取.csv文件,进行一些格式化,将每一行拆分成其列数据,并将新的分离列数据数组添加到数组列表中。然后,我想以不同的方式订购清单。目前,用户名仅按字母顺序升序。
到目前为止,这是我尝试过的:
// create list for storing arrays
List<string[]> users;
string[] lineData;
string line;
// read in stremreader
System.IO.StreamReader file = new System.IO.StreamReader("dcpmc_whitelist.csv");
// loop through each line and remove any speech marks
while((line = file.ReadLine()) != null)
{
// remove speech marks from each line
line = line.Replace("\"", "");
// split line into each column
lineData = line.Split(';');
// add each element of split array to the list of arrays
users.Add(lineData);
}
IOrderedEnumerable<String[]> usersByUsername = users.OrderBy(user => user[1]);
Console.WriteLine(usersByUsername);
这给出了一个错误:
使用未分配的局部变量“用户”
我不明白为什么要说这是一个未分配的变量?当我在Visual Studios 2010中运行程序时,为什么没有显示该列表?
因为需要在使用对象之前先创建对象,所以构造函数设置了对象,可以使用了,这就是为什么您会收到此错误的原因
用这样的东西
List<string[]> users = new List<string[]>() ;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句