参考和值类型的场景 [英] Reference and Value types scenario

查看:82
本文介绍了参考和值类型的场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩周围试图搞明白引用和值类型。就在我以为我了吧,我碰到这种情况下...

我创建了一个将包含一个对象的类。

 类容器
{
    公共对象A {获得;组;}
}
 

当我创建这个容器类(一)我创造一个引用类型的实例的实例。我分配一个整数的类中的对象。据我所知,这将是盒装作为一个对象,另一种引用类型。

  INT开始= 1;
集装箱A =新的Container {A =开始};
 

创建容器类(B)的另一实例,但分配第一容器到它的值,b的值是现在一个引用

 容器B = A;
 

正如预期的那样,当我打印出来AA和BA的价值,它们是相同的。

  Console.WriteLine(AA = {0},BA = {1},AA,BA);
//a.A=1,b.A=1
 

和,一如预期,当我改变AA的价值BA的价值也因他们引用同一个对象改变。

  A.A = 2;

Console.WriteLine(A.A = {0},b.A = {1},A.A,b.A);
// A.A = 2,b.A = 2
 

现在我决定尝试用这种个别地方的对象。再次,我框整数到第一对象和分配第一对象到第二值。我相信,对象,在这一点上,应该是一个引用类型,因此C和D应引用同一个对象。在不改变任何东西,他们返回相同的值。

  INT开始= 1;
对象C =启动;
物D = C;

Console.WriteLine(C = {0},D = {1},C,D);
// C = 1,D = 1
 

像之前,改变初始对象的值时,我期望两个对象的值是相同的。

  C = 2;

Console.WriteLine(C = {0},D = {1},C,D);
// C = 2,D = 1
 

当我打印结果为这两个对象,d的值不改变像以前一样。

有人可以解释为什么分配是在这种情况下的previous不同?

感谢

解决方案

这是你的第一个错误:

  

我创建容器类(B)的另一个实例,但将第一个值   容器到它,b的值是现在一个引用

 容器B = A;
 

这是的没有的创建另一个的实例的。它的声明另一个的变量的。现在,两个变量指向同一个对象。

(我会继续编辑这个答案,因为我一直在阅读...)

接下来:

  INT开始= 1;
对象C =启动;
物D = C;
Console.WriteLine(C = {0},D = {1},C,D); // C = 1,D = 1
 

     

像之前,改变初始对象的值时,我   预期这两个对象的值是相同的。

  C = 2;
Console.WriteLine(C = {0},D = {1},C,D); // C = 2,D = 1
 

这是不是改变了的对象的它改变的变量的。每个副本分配一个值 - 除了其中一人还执行装箱操作。让我们把它简化咯:

 对象C =第一个;
物D = C;
 

现在有没有拳击参与 - 这只会使其更容易理解

这两个的变量的目前已值指的是同一个对象。这是由于分配,但是没有什么别的连接两个变量。他们碰巧在目前的值相同,但他们的自变量。现在,让我们改变之一:

  C =不同的字符串;
 

这改变了的 C 的指代不同的对象。如果你打印出的值C D ,他们将分别为不同的字符串和第一个。 修改的 C值不改变 d值


现在,让我们回到你的previous场景,看看它为什么是不同的。在那里,你有:

  A.A = 2;
 

这是不会改变 A 的全部价值。它改变对象 A 指中的数据。

让我们用一个真实世界的比喻使它更容易些。假设所有的变量都是纸片与写在他们家的地址。 AA = 2的变化; 是像改变的内容的房子的那个地址。当然,任何人与写在同一地址的及其的一张纸上看到的变化。

现在想想你的 C / D 场景。同样,假设我们有两张纸,并且由于赋值操作符它们都具有相同的地址写在他们身上。现在,你的新值 C 变量本身赋值就像是洗了的 C 纸上的地址并写上不同的地址。这并不改变 D 一张纸 - 它仍然有旧的地址,如果你去参观一下房子,你会发现什么也没有改变。它的只是什么的写上所更改的一张纸的。

帮助吗?

I've been playing around trying to thoroughly understand Reference and Value types. Just when I thought I had it, I came across this scenario...

I created a class that would contain a single object.

class Container
{
    public object A {get; set;}
}

When I create an instance of this Container class (a) I am creating instance of a reference type. I assign an integer to the object within the class. As far as I am aware, this will be boxed as an object, another reference type.

int start = 1;  
Container a = new Container{ A=start };

I create another instance of the Container class (b), but assign the value of the first container to it, the value of b is now a reference to a.

Container b = a;

As expected when I print out the value of both a.A and b.A, they are the same.

Console.WriteLine("a.A={0},b.A={1}",a.A,b.A);
//a.A=1,b.A=1

And, as expected, when I change the value of a.A the value of b.A also changes due to them referencing the same object.

a.A = 2;

Console.WriteLine("a.A={0},b.A={1}",a.A,b.A);
// a.A=2,b.A=2

Now I decided to try this using individual local objects. Again, I box the integer into the first object and assign the value of the first object to the second. I believe the object, at this point, should be a reference type so c and d should reference the same object. Without changing anything they return the same value.

int start = 1;
object c = start;
object d = c;

Console.WriteLine("c={0},d={1}",c,d);
// c=1,d=1

Like before, when changing the value of the initial object, I expect the value of both objects to be the same.

c = 2;

Console.WriteLine("c={0},d={1}",c,d);
// c=2,d=1

When I print the result for these two objects, the value of d does not change like before.

Can someone please explain why the assignment is different in this scenario to the previous?

Thanks

解决方案

Here's your first mistake:

I create another instance of the Container class (b), but assign the value of the first container to it, the value of b is now a reference to a.

Container b = a;

That's not creating another instance. It's declaring another variable. Now both variables refer to the same object.

(I'll keep editing this answer as I keep reading...)

Next up:

int start = 1;
object c = start;
object d = c;
Console.WriteLine("c={0},d={1}",c,d); // c=1,d=1

Like before, when changing the value of the initial object, I expect the value of both objects to be the same.

c = 2;
Console.WriteLine("c={0},d={1}",c,d); // c=2,d=1

That's not changing an object it's changing a variable. Each of the assignments copies a value - except one of them also performs a boxing operation. Let's simplify it slightly:

object c = "first string";
object d = c;

Now there's no boxing involved - it'll just make it simpler to understand.

Both variables currently have values referring to the same object. That's due to the assignment, but there's nothing else linking the two variables. They happen to have the same value at the moment, but they're independent variables. Now let's change one:

c = "different string";

That has changed the value of c to refer to a different object. If you print out the values of c and d they will be "different string" and "first string" respectively. Changing the value of c doesn't change the value of d.


Now, let's go back to your previous scenario to see why it's different. There, you had:

a.A = 2;

That isn't changing the value of a at all. It's changing the data within the object a refers to.

Let's use a real world analogy to make this easier. Suppose all our variables are pieces of paper with house addresses written on them. The change of a.A = 2; is like changing the contents of the house at that address. Of course anyone else with the same address written on their piece of paper will see the change.

Now think of your c/d scenario. Again, imagine that we have two pieces of paper, and due to the assignment operator they both have the same address written on them. Now your assignment of a new value to the c variable itself is like scrubbing out the address on the c piece of paper and writing a different address on it. That doesn't change the d piece of paper at all - it still has the old address on, and if you visit that house, you'll see that nothing's changed. It's only what's written on the piece of paper that's changed.

Does that help?

这篇关于参考和值类型的场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆