开发者

References to direct object instance, not array's cell c#

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[开发者_如何学Go1]. 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?


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.


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜