尝试在C#中使用IOrderedEnumerable命令列表

crm*_*ham 0 c# sorting console-application visual-studio-2010 unassigned-variable

我试图读取.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);
Run Code Online (Sandbox Code Playgroud)

这给出了一个错误:

使用未分配的本地变量'users'

我不明白为什么它说它是一个未分配的变量?为什么在Visual Studio 2010中运行程序时,列表不显示?

BRA*_*mel 5

因为在使用之前需要创建对象,所以构造函数设置对象,准备使用这就是为什么会出现此错误

使用这样的东西

List<string[]> users = new List<string[]>() ; 
Run Code Online (Sandbox Code Playgroud)