Directly increasing an int in a List of objects
List<object> objects = new List<object>();
objects.Add(5);
I want to do
objects[0] += 10;
But I need to cast it first.
int a = (int) objects[0];
a += 10;
But doing this only changes a, not the integer in the list.
开发者_开发问答What's the best way to solve this?
You could do
objects[0] = ((int)objects[0]) + 10;
You can't directly increase the boxed integer that is stored in the list, because boxed structs are immutable. Jack Edmonds' solution is about as close as you can get.
have a list of integers instead ;p
List<int> objects = new List<int>();
精彩评论