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

user1929393

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.amount, myMoney.type);

Here is my code:

static void Main(string[] args)
{
    List<Money> myMoney = new List<Money> 
    {
        new Money{amount = 10, type = "US"},
        new Money{amount = 20, type = "US"}
    };
}

class Money
{
    public int amount { get; set; }
    public string type { get; set; }
}
Simon Whitehead

foreach:

foreach (var money in myMoney) {
    Console.WriteLine("Amount is {0} and type is {1}", money.amount, money.type);
}

MSDN Link

Alternatively, because it is a List<T>.. which implements an indexer method [], you can use a normal for loop as well.. although its less readble (IMO):

for (var i = 0; i < myMoney.Count; i++) {
    Console.WriteLine("Amount is {0} and type is {1}", myMoney[i].amount, myMoney[i].type);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can i loop each item of list in each column?

From Dev

How to loop through a function for each item in list?

From Dev

Loop through a foreach and grab each date item LINQ

From Dev

How can I run a loop through 2 lists simultaneously so each item in the loops match up? [Python]

From Dev

How can I loop through a jquery array and then output each item (starting at index 0) one at a time?

From Dev

How can I add a button after each list item in a Jade loop?

From Dev

How can I add a button after each list item in a Jade loop?

From Dev

In Python, how do I grab the value of an entry in a python list created through a loop

From Dev

In Python, how do I grab the value of an entry in a python list created through a loop

From Dev

How do I loop through each string in my list?

From Dev

How do I loop through each string in my list?

From Dev

How do I set the names of a list on each iteration through a loop

From Dev

how to loop through each item of a MongoCollection?

From Dev

How can I loop through each character of a string?

From Dev

How can I target each item in a list with collapsible items?

From Dev

How can I position image before each item in inline list?

From Dev

How can I target each item in a list with collapsible items?

From Dev

Linq - How can I run each item through a function and then pick the item with the highest function result

From Dev

How can I create buttons for every item in a list using a for loop?

From Dev

How to append each item in the list using loop?

From Dev

Loop for each item in a list

From Dev

How can I loop through all checked checkboxes in jQuery, getting their value in each loop?

From Dev

How do I loop through a subscribed collection in re-frame and display the data as a list-item?

From Dev

How to loop through an array in waiting a time between each item?

From Dev

How to cycle through each item in a hashmap using a while loop

From Dev

How can I grab the last element of a List of string arrays?

From Dev

How can I grab the last element of a List of string arrays?

From Dev

Firebase for Android, How can I loop through a child (for each child = x do y)

From Dev

How can I loop through an array object and check each element to see if one is defined?

Related Related

  1. 1

    How can i loop each item of list in each column?

  2. 2

    How to loop through a function for each item in list?

  3. 3

    Loop through a foreach and grab each date item LINQ

  4. 4

    How can I run a loop through 2 lists simultaneously so each item in the loops match up? [Python]

  5. 5

    How can I loop through a jquery array and then output each item (starting at index 0) one at a time?

  6. 6

    How can I add a button after each list item in a Jade loop?

  7. 7

    How can I add a button after each list item in a Jade loop?

  8. 8

    In Python, how do I grab the value of an entry in a python list created through a loop

  9. 9

    In Python, how do I grab the value of an entry in a python list created through a loop

  10. 10

    How do I loop through each string in my list?

  11. 11

    How do I loop through each string in my list?

  12. 12

    How do I set the names of a list on each iteration through a loop

  13. 13

    how to loop through each item of a MongoCollection?

  14. 14

    How can I loop through each character of a string?

  15. 15

    How can I target each item in a list with collapsible items?

  16. 16

    How can I position image before each item in inline list?

  17. 17

    How can I target each item in a list with collapsible items?

  18. 18

    Linq - How can I run each item through a function and then pick the item with the highest function result

  19. 19

    How can I create buttons for every item in a list using a for loop?

  20. 20

    How to append each item in the list using loop?

  21. 21

    Loop for each item in a list

  22. 22

    How can I loop through all checked checkboxes in jQuery, getting their value in each loop?

  23. 23

    How do I loop through a subscribed collection in re-frame and display the data as a list-item?

  24. 24

    How to loop through an array in waiting a time between each item?

  25. 25

    How to cycle through each item in a hashmap using a while loop

  26. 26

    How can I grab the last element of a List of string arrays?

  27. 27

    How can I grab the last element of a List of string arrays?

  28. 28

    Firebase for Android, How can I loop through a child (for each child = x do y)

  29. 29

    How can I loop through an array object and check each element to see if one is defined?

HotTag

Archive