直接增加对象列表中的 int

发布于 2024-12-09 16:14:59 字数 286 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一城柳絮吹成雪 2024-12-16 16:14:59

你可以做

objects[0] = ((int)objects[0]) + 10;

You could do

objects[0] = ((int)objects[0]) + 10;
难理解 2024-12-16 16:14:59

您不能直接增加存储在列表中的装箱整数,因为装箱结构是不可变的。杰克·埃德蒙兹的解决方案几乎是您能得到的最接近的解决方案。

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.

悲欢浪云 2024-12-16 16:14:59

有一个整数列表;p

List<int> objects = new List<int>();

have a list of integers instead ;p

List<int> objects = new List<int>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文