I have a problem that seems like it ought to be really simple, but amazingly I can't figure out a good solution to it. Perhaps I'm missing something obvious.
I have a list of objects that I wish to loop through multiple times, test against, and tally up how many times the objects fulfill the criteria in my test, like so:
var tallies = new List<Tuple<object, int>>();
foreach (var talliedObject in myObjects) {
// Initialize
tallies.add(new Tuple<object, int>(talliedObject, 0));
}
while (...) {
updateTallies(tallies, stuffToCheckAgainst);
}
But the problem is, I can't just say thisTuple.Item2++
to increment my tally because a Tuple's values are immitable. So is a KeyValuePair
's value. Apparently this is all by design because "mutable structs are evil". Why are they evil in this case? Do I really have to create a custom class to store my object along with an associated integer that I can actually modify?