I have ten positions with x, y and name. I would like to set all x to x+1. How to do this? I tried different methods, but I always get that I cannot modify the list. It should be done with for or foreach.
I get "Cannot modify members of "item" because it is foreach iteration variable."
public struct Position
{
public int x { get; set; }
public int y{ get; set; }
public string name{ get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Position> positions = new List<Position>();
Position position = new Position();
position.x = 12;
position.y = 12;
position.name = "left";
positions.Add(position);
position = new Position();
position.x = 13;
position.y = 13;
position.name = "right";
positions.Add(position);
Console.WriteLine(positions.Count);
Console.WriteLine();
Position newPosition;
foreach (Position item in positions)
{
}
Console.WriteLine();
foreach (Position item in positions)
{
Console.WriteLine(item.x + " " + item.name);
}
}
}
}