C
C#10mo ago
Ace Guy

✅ Can't modify a list outside of the cycle

So I'm trying to modify differenceList in a cycle, but it gives me an error (see attachment), although differenceList isn't a part of the cycle and shouldn't be a problem //please ignore my poor variable naming
No description
13 Replies
Ace Guy
Ace GuyOP10mo ago
if i remove differenceList.add(obj); then it doesn't crash
Angius
Angius10mo ago
What's differenceList?
Ace Guy
Ace GuyOP10mo ago
a string HashSet
Angius
Angius10mo ago
Are you looping over it as well?
Ace Guy
Ace GuyOP10mo ago
I don't think so
Angius
Angius10mo ago
The error would happen only if you modify input inside of the loop So I wonder if, perhaps, differenceList holds a reference to input
Ace Guy
Ace GuyOP10mo ago
well there is differenceList = input; before the cycle starts, that could be the problem brb
Angius
Angius10mo ago
Yes, that means differenceList and input are one and the same differenceList is just an alias for input
Ace Guy
Ace GuyOP10mo ago
ooooooooh
Angius
Angius10mo ago
So by modifying the former, you modify the latter
Ace Guy
Ace GuyOP10mo ago
how can I make the contents the same but not link them like that?
Angius
Angius10mo ago
If it's a hashset, try .ToHashSet()
differenceList = input.ToHashSet();
differenceList = input.ToHashSet();
Ace Guy
Ace GuyOP10mo ago
oh yeah that seems to have worked, tysm!
Want results from more Discord servers?
Add your server