This is mainly a theoretical question i.e I don’t have any need for a practical solution but when using foreach in MS V C# 2010 with the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] ints = new int[7]{0,0,0,0,0,0,0};
foreach (int i in ints)
{
i = 10;
}
Console.ReadKey();
}
}
}
I get the error which tells me that I cannot modify i because it is a foreach variable,now if I wanted to I could do this simple task a number of other ways but the thing is I haven’t seen anything in the documentation which would disallow what I’m trying to do and I think foreach should give you the ability to change the variable within the list.
Is there something I’m missing?
Use a for loop instead and modify the values using ints[i]