对直接对象实例的引用,而不是数组的单元格 C#
我有对象数组(假设类名称是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
foo
的值是对对象的直接引用...但goo[1]
也是如此。它们引用同一个对象,因此如果您改变该对象(无论您如何操作),这些更改都将通过两个引用看到。请注意,通过 foo 更改值实际上并没有更改
foo
的值。我喜欢用房屋(作为对象)、街道地址(作为参考)和纸张(作为变量)来思考它。假设我把我的地址写在两张纸上,一张给你,一张给别人(弗雷德)。然后你把我的房子漆成红色,然后弗雷德来拜访(通过阅读纸上写的地址)。你改变了我房子的颜色——但你根本没有改变弗雷德的纸。如果您想避免这种别名,您应该考虑使您的类型不可变,并提供创建新对象的方法,这些新对象与旧对象类似,除了一些特定的更改 - 例如
String
所做的。The value of
foo
is a direct reference to the object... but so isgoo[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 offoo
. 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.没有直接(安全)的方法来创建对 .NET 数组中元素的“地址”之类的引用。但是,您可以使用委托而不是引用,并编写如下内容:
现在,
foo
是一个函数,您可以评估它以获取索引处的对象。您可以这样使用它:如果您现在对
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:
Now,
foo
is a function that you can evaluate to get the object at the index. You can use it like this: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 differentSnap
)你在这里错过了重点。
将
goo
声明为对Snap
实例的引用数组。也就是说,goo
的每个元素都是引用,而不是Snap
的实例。现在,您已将
foo
声明为对Snap
实例的引用,并且它恰好引用了与以下内容相同的Snap
实例:是goo[1]
。更准确地说,如果您更改
foo
引用的实例,它也会更改goo[1]
引用的实例(因为它们都引用同一个实例) )。这意味着您正在更改每个元素引用的 Snap 实例。由于您从未告诉 foo 也更改它所引用的 Snap 实例,因此它当然仍然引用它最初引用的同一实例。
You're missing the point here.
declares
goo
as an array of references to instances ofSnap
. That is, each element ofgoo
is a reference, not an instance ofSnap
.Now, you've declared
foo
as a reference to an instance ofSnap
, and it happens to be referring to the same instance ofSnap
as isgoo[1]
.More precisely, if you change the instance referred to by
foo
, it will also change the instance referred to bygoo[1]
(since they are both referring to the same instance).This means that you are changing the instance of
Snap
that each of the elements refer to. Since you never toldfoo
to also change the instance ofSnap
that it is referring to, of course it still refers to the same instance it initially referred to.