Search code examples
c#visual-studioconsole-applicationgeneric-list

How to display items in a list with a description of what that list item is in c# console


I am trying to display a list sorted in descending order with each item being displayed with a description of what the value of the item is in VS console app. I’m just not sure how to display the description with each item

Eg. output: Total monthly expenses//description of list item : $1000//item obtained from list and sorted Home loan repayment: $700 Vehicle Installment: $300

Thank you


Solution

  • I cannot see where you are getting the description values from. A better way to do this would be to use Dictionary instead of a List.

    double cost = textbox1.Text; // cost of item
    string des = textbox2.Text; //description of item
    
    //below code goes into a event to add each item cost+description
    Dictionary<double, string> io = new Dictionary<double, string>();
    io.Add(cost, des);
    
    //below code goes into a event to display all items
    foreach(KeyValuePair<double, string> val in io.OrderByDescending(i => i.Key)) {
    Console.WriteLine("${0},{1}", val.Key, val.Value);
    }