如何在列表中显示项目,并说明该列表项目在 C# 控制台中的内容

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

我正在尝试显示一个按降序排序的列表,其中显示的每个项目都带有对项目在 VS 控制台应用程序中的值的描述。我只是不确定如何显示每个项目的描述

例如。输出: 每月总费用//列表项描述:1000美元//从列表中获取并排序的项目 房屋贷款还款:$700 车辆分期付款:$300

谢谢

我看不到您从哪里获取描述值。更好的方法是使用字典而不是列表。

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("[=10=],{1}", val.Key, val.Value);
}

假设您已经从数据库中获取数据,将数据存储在字典集合中。字典集合key-value对中的key为商品描述信息,value为具体价格。可以先提取字典集合中key的值进行排序,再根据key的值提取字典集合中的value值

static void main(string[] args)
{
    Dictionary<string, int> d = new Dictionary<string, int>();

    d.Add("keyboard", 100);
    d.Add("mouse", 80);

    //get key
    var val = d.Keys.ToList();

    //sort
    val.Sort();

   
    foreach (var key in val)
    {
        Console.WriteLine(key);
    }
}