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

前端 未结 4 1296
难免孤独
难免孤独 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

    Just like any other collection. With the addition of the List.ForEach method.

    foreach (var item in myMoney)
        Console.WriteLine("amount is {0}, and type is {1}", item.amount, item.type);
    
    for (int i = 0; i < myMoney.Count; i++)
        Console.WriteLine("amount is {0}, and type is {1}", myMoney[i].amount, myMoney[i].type);
    
    myMoney.ForEach(item => Console.WriteLine("amount is {0}, and type is {1}", item.amount, item.type));
    

提交回复
热议问题