xxxxxxxxxx
var countries = new []{
new { Name = "United States", Abbr = "US", Currency = "$" },
new { Name = "Canada", Abbr = "CA", Currency = "$" }
};
foreach(var country in countries)
{
var Name = country.Name;
..
}
xxxxxxxxxx
List<string> myList = new List<string>{"Item 1", "Item 2", "Item 3"};
// Using a foreach loop
foreach(var item in myList)
{
// Process each item
Console.WriteLine(item);
}
// Using a for loop
for(int i = 0; i < myList.Count; i++)
{
// Process each item at index i
Console.WriteLine(myList[i]);
}
xxxxxxxxxx
//lets say we have a list of ints
List<int> test_nums = new List<int>{ 1, 2, 3, 4, 5, 6 };
//now lets say we want to iterate through the list and print the ints one by one
//well, for that we can use the foreach method, that lets us iterate through a data structure
//and get his value at a specific index each iteration like in an array
//(an example of the value in an array in each index: array[0], array[1] ... array[length-1])
//the first one:
foreach(int i in test_nums)
{
Console.WriteLine(i);
}
//Here we just iterate through the list using foreach nad print to the screen
//the number its iteration.