简体   繁体   English

原始值更改未反映在引用类型变量中

[英]Original Values changes are not reflecting in reference types variable

object a = "1411";
object b = a;

Console.WriteLine("Before Value of a " + a);
Console.WriteLine("Before Value of b " + b);

a = "5555";

Console.WriteLine("After Value of a " + a);
Console.WriteLine("After Value of b " + b);

Console.ReadKey();

output: 输出:

Before Value of a 1411 价值1411之前

Before Value of b 1411 b的前值1411

After Value of a 5555 5555后值

After Value of b 1411 b的后值1411

After Value of b also should changed to 5555 right? 之后b的值也应该改为5555对吗? since b is reference types variable. 因为b是引用类型变量。

Let's do this with numbers: 让我们用数字来做到这一点:

int x = 1411; // now x is 1411
int y = x; // now y is 1411

x = 5555; // now x is 5555

Console.WriteLine(y);

Now: what is y ? 现在: y是什么? Simple: it is still 1411 . 很简单:仍然是1411 Assigning a new value to x doesn't change y . x赋一个新值不会改变y The same is true with reference-types, but simply the "value" of a reference-type variable is the reference . 引用类型也是如此,只是引用类型变量的“值” 就是reference Not the object. 不是对象。

If you assign a reference-type variable to be a different value (ie to point at a different object), that only affects that single variable. 如果将引用类型变量分配为其他值 (即指向其他对象),则只会影响该单个变量。

Now, if you did: 现在,如果您这样做:

var x = new SomeClass { Foo = "1411" };
var y = x;

x.Foo = "5555";

Console.WriteLine(y.Foo);

Then this would print "5555". 那么将打印“5555”。 The difference now is that we have one object, and both reference-type variables point at the same object. 现在的区别是我们只有一个对象,并且两个引用类型的变量都指向同一对象。 We have changed a value of the object (not the reference), so updating that changes it no matter how you get to the same object. 我们更改了对象 (而不是引用)的值,因此无论您如何访问同一对象,对其进行更新都会对其进行更改。

Let's take this code piece by piece to see what it does: 让我们逐步看一下这段代码,看看它的作用:

a = "1411";

This will store a reference to an object into the variable a . 这会将对对象的引用存储到变量a The object is a string , and allocated on the heap (since it's a reference type). 该对象是一个string ,并且在堆上分配(因为它是引用类型)。

So there are two pieces involved here: 因此,这里涉及两个部分:

  • The variable a 变量a
  • The object (string) that it refers to 它引用的对象(字符串)

Then we have this: 然后我们有这个:

b = a;

This will make the variable b reference the same object that a refers to. 这将使得变量b引用相同的对象的是a指。

References internally are implemented as memory addresses, and thus if (example) the string object lives at address 1234567890, then the values of the two variables would both be that address. 内部引用是作为内存地址实现的,因此,如果(例如)字符串对象位于地址1234567890,则两个变量的值都将是该地址。

Now, then you do this: 现在,您可以执行以下操作:

a = "5555";

This will change the contents of the a variable, but the b variable will be left unchanged. 这将更改a变量的内容,但b变量将保持不变。

This means that b still refers to the old object, at address 1234567890, whereas a will refer to a different string object. 这意味着b仍然引用旧对象,地址为1234567890,而a将引用其他字符串对象。

You did not change the object itself, that both a and b were referring to, you changed a . 您没有更改ab都引用的对象本身,而是更改a

As Marc said in a comment, you can liken this to giving you the address of a house on a piece of paper. 正如Marc在评论中所说,您可以将其比作在纸上给房子的地址。 If you give a piece of paper to your friend, writing up the same address on that second piece of paper, you are referring to the same house on both. 如果您给朋友一张纸,在第二张纸上写相同的地址,那么您指的是两个人的同一个房子。

However, if you give your friend a paper with a different address on it, even if the two houses looks the same, they're not the same house . 但是,如果您给您的朋友写有不同地址的纸张,即使两所房子看起来相同,它们也不是同一所房子

So there's a big difference between reference type and variable containing a reference . 因此, 引用类型包含引用的变量之间存在很大差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM