IEnumerable, changes to contained class not applied
Hey,
So I have this small code:
Where basically I'm just creating data, setting some values to 0 and then printing them
But all my prints show me non-zero values
Am I missing something obvious?
9 Replies
the problem you're seeing here is related to multiple enumeration.
while
foreach (var s in slots) s.SpaceLeft = 0;
does set the value accordingly, you are then re-enumerating the return value of the Select
, which will, once again, set the values back to what you specified in the Select
it's a bit of a doozie if you don't know how IEnumerable
s workHmm I was thinking it would been applied since info were stored inside class
Should I just do a ToArray to stop it from going back to it then?
yes, a ToArray call would solve this, for example
maybe this helps visualize it a little bit
Amazing, thanks a lot
$close
If you have no further questions, please use /close to mark the forum thread as answered
@Zirk Rider would also spit out a warning here
VS maybe too. What are you using?
I was using VS