C#C
C#2y ago
olsu

List<> modification

I want ot modify all elements in a List<>, but im having trouble. This is my code. Which gives an error saying I cannot modify vertex because it is a 'foreach iteration variable'
public struct Vertex
{
    public float life;
};

class Program
{
    public static void Main(string[] args)
    {
        List<Vertex> vertices = new List<Vertex>();
        vertices.Add(new Vertex());

        foreach (var vertex in vertices)
        {
          vertex.life += 1;
        }
  }
}

I come from c++, so I can guess that the problem is that var vertex is copied and not actually what i want to modify. Is there a way to get the actual instance of the vertex in c#?
Was this page helpful?