© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
72 replies
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
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
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#?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

UI modification
C#CC# / help
2y ago
excel modification
C#CC# / help
2y ago
Return Value on Entity Modification
C#CC# / help
2y ago