[Solved] C# foreach loop take values from outer loop to inner loop


try

for (int i = 0; i < a.Length; i++) // or i < n if you want
{
    print i;
    int z = a[i]; // this line will get value from a one by one, 0, 1, 2, 3 and so on...
}

Edit 1 –

After seeing the comments on the other answer, the array ‘a’ turns out is a dynamic array which have size n (which is 2)

the revised edition:

int n = 2;
int[] a = new int[n];
string input = null;

for (int i = 0; i < a.Length; i++) // or i < n if you want
{
    print i;
    input = Console.ReadLine();
    try {
        a[i] = int.Parse(input);
        Console.WriteLine(string.Format(
            "You have inputted {0} for the {1} element",
            input, i
        ));
    } catch { Console.WriteLine("Non integer input"); i -= 1; }
}

solved C# foreach loop take values from outer loop to inner loop