c# foreach loop array code example
Example 1: c# loop through array
for (int i = 0; i < arr.Length; i++)
{
}
foreach (var element in arr)
{
}
Example 2: c# foreach arra
int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 } };
foreach (int i in numbers2D)
{
System.Console.Write("{0} ", i);
}
Example 3: c# foreach
var numbers = new NumberList<int> { 0, 1, 2, 3, 4, 5 };
foreach (int num in numbers)
{
DoSomething();
}
Example 4: c# loop though array
int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 };
foreach (int i in numbers)
{
System.Console.Write("{0} ", i);
}