Search code examples
c#visual-studio-2010sortingconsole-applicationunassigned-variable

Attempting to order a list using IOrderedEnumerable in C#


I am attempting to read in a .csv file, do some formatting, split each line up into its column data and add the new array of seperated column data in to a list of arrays. Then I want to order the list in different ways. Currently just by username ascending alphabetically.

This is what I have attempted so far:

// 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);

This gives one error:

Use of unassigned local variable 'users'

I don't understand why it is saying it is an unassigned variable? Why does the list not show when I run the program in Visual studios 2010?


Solution

  • Because objects need to be created before being used, The constructor sets up the object, ready for use this why you get this error

    use something like this

    List<string[]> users = new List<string[]>() ;