C
C#2y 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
Thinker2y ago
You pretty much have to use indices Lists specifically do not allow you to modify them during enumeration
hime
hime2y ago
In what way do you want to modify the object?
Sound
SoundOP2y ago
in any way let s say to move it to the end
hime
hime2y 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
Accord2y 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.

Did you find this page helpful?