How can I loop through a List and grab each item?

前端 未结 4 1297
难免孤独
难免孤独 2020-11-28 23:02

How can I loop through a List and grab each item?

I want the output to look like this:

Console.WriteLine(\"amount is {0}, and type is {1}\", myMoney         


        
4条回答
  •  孤城傲影
    2020-11-28 23:31

    This is how I would write using more functional way. Here is the code:

    new List()
    {
         new Money() { Amount = 10, Type = "US"},
         new Money() { Amount = 20, Type = "US"}
    }
    .ForEach(money =>
    {
        Console.WriteLine($"amount is {money.Amount}, and type is {money.Type}");
    });
    

提交回复
热议问题