对直接对象实例的引用,而不是数组的单元格 C#

发布于 2024-10-08 07:59:17 字数 411 浏览 1 评论 0原文

我有对象数组(假设类名称是 Snap)Snap。

有没有办法保存对单元格中对象的变量直接引用,而不是单元格? 因为,当我这样做时:

Snap[] goo = new Snap[3];
// here we add some objects to this array

Snap foo = (Snap)goo[1];

所以现在,如果我改变 foo 那么我也会改变 goo[1]。好的。但现在我将对 goo 进行一些排列:

first i had: 0 1 2
after permutation: 1 0 2

foo 仍然是 goo[1] 但不是这个对象!该对象现在位于 goo[0] 中! 那么,如何直接“捕获”这个对象,而不是细胞! 有什么想法吗?

I have array of Objects (let say that class name is Snap) Snap.

Is there any way to save in variable direct reference to object in cell, not cell?
Coz, when I'm doing like this:

Snap[] goo = new Snap[3];
// here we add some objects to this array

Snap foo = (Snap)goo[1];

So now, if I will change foo then i Will change also goo[1]. Ok. But now I i will make some permutations on goo:

first i had: 0 1 2
after permutation: 1 0 2

The foo will be still goo[1] but not this object! This object is now in goo[0] !
So, how to "catch" this object directly, not cell!
Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

铃予 2024-10-15 07:59:17

foo 的值是对对象的直接引用...但 goo[1] 也是如此。它们引用同一个对象,因此如果您改变该对象(无论您如何操作),这些更改都将通过两个引用看到。

请注意,通过 foo 更改值实际上并没有更改 foo。我喜欢用房屋(作为对象)、街道地址(作为参考)和纸张(作为变量)来思考它。假设我把我的地址写在两张纸上,一张给你,一张给别人(弗雷德)。然后你把我的房子漆成红色,然后弗雷德来拜访(通过阅读纸上写的地址)。你改变了我房子的颜色——但你根本没有改变弗雷德的纸。

如果您想避免这种别名,您应该考虑使您的类型不可变,并提供创建新对象的方法,这些新对象与旧对象类似,除了一些特定的更改 - 例如 String 所做的。

The value of foo is a direct reference to the object... but so is goo[1]. They refer to the same object, so if you mutate the object (however you do it) those changes will be seen via both references.

Note that changing a value via foo is not actually changing the value of foo. I like to think about it in terms of houses (as objects), street addresses (as references) and pieces of paper (as variables). Suppose I write my address down on two pieces of paper, and give one to you and one to someone else (Fred). You then paint my house red, and then Fred comes to visit (by reading the address written on the piece of paper). You have changed the colour of my house - but you haven't changed Fred's piece of paper at all.

If you want to avoid this sort of aliasing, you should look into making your types immutable and provide methods to create new objects which are like the old objects except for some specific change - like String does, for example.

鹤仙姿 2024-10-15 07:59:17

没有直接(安全)的方法来创建对 .NET 数组中元素的“地址”之类的引用。但是,您可以使用委托而不是引用,并编写如下内容:

Snap[] goo = new Snap[3];
Func<Snap> foo = () => (Snap)goo[1];

现在,foo 是一个函数,您可以评估它以获取索引处的对象。您可以这样使用它:

foo().SomeSnapProperty = 10;

如果您现在对 goo 对象进行排列,然后再次运行上述语句,它将在索引 1 处为您提供新对象(因此您将修改不同的 <代码>快照)

There is no direct (safe) way of creating reference to something like "address" of an element in a .NET array. However, you can use delegates instead of references and write something like:

Snap[] goo = new Snap[3];
Func<Snap> foo = () => (Snap)goo[1];

Now, foo is a function that you can evaluate to get the object at the index. You can use it like this:

foo().SomeSnapProperty = 10;

If you now do a permutation of goo objects and then run the above statement again, it will give you the new object at index 1 (so you'll modify a different Snap)

终难愈 2024-10-15 07:59:17

你在这里错过了重点。

Snap[] goo = new Snap[3];

goo 声明为对 Snap 实例的引用数组。也就是说,goo 的每个元素都是引用,而不是Snap实例

Snap foo = goo[1];

现在,您已将 foo 声明为对 Snap 实例的引用,并且它恰好引用了与以下内容相同的 Snap 实例:是goo[1]

所以现在,如果我改变 foo 那么我也会改变 goo[1]。

更准确地说,如果您更改 foo 引用的实例,它也会更改 goo[1] 引用的实例(因为它们都引用同一个实例) )。

但现在我会在 goo 上做一些排列

这意味着您正在更改每个元素引用的 Snap 实例。由于您从未告诉 foo 也更改它所引用的 Snap 实例,因此它当然仍然引用它最初引用的同一实例。

You're missing the point here.

Snap[] goo = new Snap[3];

declares goo as an array of references to instances of Snap. That is, each element of goo is a reference, not an instance of Snap.

Snap foo = goo[1];

Now, you've declared foo as a reference to an instance of Snap, and it happens to be referring to the same instance of Snap as is goo[1].

So now, if I will change foo then i Will change also goo[1].

More precisely, if you change the instance referred to by foo, it will also change the instance referred to by goo[1] (since they are both referring to the same instance).

But now I i will make some permutations on goo

This means that you are changing the instance of Snap that each of the elements refer to. Since you never told foo to also change the instance of Snap that it is referring to, of course it still refers to the same instance it initially referred to.

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