olsu
olsu
CC#
Created by olsu on 6/1/2024 in #help
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;
}
}
}
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#?
73 replies