A foreach loop doesn't give you an index value, it gives you the value of the element. In your case, the first element is 1, which you then used like an index to get 2.
Here's your code with some additional information in the WriteLine method to illustrate what's going on
int[] MyArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
foreach (int i in MyArray)
{
Console.WriteLine($"i is '{i}', MyArray[i] is '{MyArray[i]}'");
}
i is '1', MyArray[i] is '2'
i is '2', MyArray[i] is '3'
i is '3', MyArray[i] is '4'
i is '4', MyArray[i] is '5'
i is '5', MyArray[i] is '6'
i is '6', MyArray[i] is '7'
i is '7', MyArray[i] is '8'
i is '8', MyArray[i] is '9'
i is '9', MyArray[i] is '0'
i is '0', MyArray[i] is '1'
For the sake of avoiding confusion, don't name the variable of a foreach loop i. Call it something that better represents each element of your collection.
foreach (var number in MyArray)
{
Console.WriteLine(number);
}
This is different than a for loop, which (most of the time) works with index values
// this is why people call the for loop variable "i"
for (int index = 0; index < myArray.Length; index++)
{
var number = myArray[index];
}
Fun fact, there is a way to get the index value with a foreach loop, but it's more advanced and uses LINQ.
foreach (var data in MyArray.Select((x, i) => new { Value = x, Index = i}))
{
var number = data.Value;
var index = data.Index;
}
Don't worry about this though, it's only necessary if you really need it.