C
C#13mo ago
Sound

❔ Foreach

in c++ i can do :
auto objects = std::vector<Object>();
for(auto& obj : objects) {...} // here i can modify obj (move, remove etc)
for(auto obj : objects) {...} // here i can't move/delete obj from objects
auto objects = std::vector<Object>();
for(auto& obj : objects) {...} // here i can modify obj (move, remove etc)
for(auto obj : objects) {...} // here i can't move/delete obj from objects
From what i know, in C# this doesn t allow me to remove, move, modify objects.
var objects = new List<Object>();
foreach(var obj in objects) {...}
var objects = new List<Object>();
foreach(var obj in objects) {...}
How can i do something like this for(auto& obj : objects) without having to use indices?
5 Replies
Thinker
Thinker13mo ago
You pretty much have to use indices Lists specifically do not allow you to modify them during enumeration
hime
hime13mo ago
In what way do you want to modify the object?
Sound
Sound13mo ago
in any way let s say to move it to the end
hime
hime13mo ago
I ask this because there's several ways you could do things when it's vague, for example doing LINQ to map the list to what you want Move the first object to the end? So you mean a Queue<T> would fit better
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.