直接增加对象列表中的 int
List<object> objects = new List<object>();
objects.Add(5);
我想做,
objects[0] += 10;
但我需要先投射它。
int a = (int) objects[0];
a += 10;
但这样做只会改变 a,而不是列表中的整数。
解决这个问题的最佳方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以做
You could do
您不能直接增加存储在列表中的装箱整数,因为装箱结构是不可变的。杰克·埃德蒙兹的解决方案几乎是您能得到的最接近的解决方案。
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.
有一个整数列表;p
have a list of integers instead ;p