Lyce
12
Q:

c# loop through array

//iterate the array
for (int i = 0; i < arr.Length; i++)
{
  // loop ...
}
// or
foreach (var element in arr)
{
  // loop ...
}
2
 string[] arr = new string[4]; // Initialize.
        arr[0] = "one";               // Element 1.
        arr[1] = "two";               // Element 2.
        arr[2] = "three";             // Element 3.
        arr[3] = "four";              // Element 4.

        // Loop over strings.
        foreach (string s in arr)
        {
            Console.WriteLine(s);
        }
1
int[] numberArray = { 4, 5, 6, 1, 2, 3, -2, -1, 0 };
foreach (int number in numberArray)
{
    System.Console.Write("{0} ", number);
}
4
int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 };
foreach (int i in numbers)
{
    System.Console.Write("{0} ", i);
}
// Output: 4 5 6 1 2 3 -2 -1 0
0
int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 } };
// Or use the short form:
// int[,] numbers2D = { { 9, 99 }, { 3, 33 }, { 5, 55 } };

foreach (int i in numbers2D)
{
    System.Console.Write("{0} ", i);
}
// Output: 9 99 3 33 5 55
0
 for (int i = 0; i < theData.Length - 2; i+=3) 
    { 

        // use theData[i], theData[i+1], theData[i+2]

    } 
0

New to Communities?

Join the community