Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a simple misunderstanding of references and how they work, and what variables are.</p> <pre><code>object a; // a is a storage location // it holds references to instances of object object b; // b is a storage location //it holds references to instances of object b = 5; // "boxes" 5 into an instance of object // and assigns reference to that object to b a = b; // assigns reference in storage location b to storage location a b = 2; // "boxes" 2 into an instance of object // and assign reference to that object to b </code></pre> <p>Think of it like this. <code>a</code> and <code>b</code> are pieces of paper that hold addresses to homes on them. When you say <code>b = 5</code>, think of it as writing down the address to a home <code>5</code> on the piece of paper <code>b</code>. When you say <code>a = b</code>, think of it as <em>copying</em> the address that is written on <code>b</code> to <code>a</code>. When you say <code>b = 2</code>, think of it as <em>erasing</em> the address that is written on <code>b</code> and replacing it with the address to home <code>2</code>. <strong>This action does not change the value that is written on the piece of paper <code>a</code>.</strong> That's what is happening here.</p> <p>Now, let's look at a very simple way to make what you're trying to do work.</p> <pre><code>class MyValue { public int Value { get; set; } } MyValue b = new MyValue { Value = 5 }; MyValue a = b; b.Value = 2; </code></pre> <p>Now, if you say</p> <pre><code>Console.WriteLine(a.Value); </code></pre> <p>what will happen? Let's reason carefully. Again, back to the analogy of <code>a</code> and <code>b</code> as pieces of paper with addresses written on them. We have said <code>MyValue b = new MyValue { Value = 5 }</code>. Think of this as writing down on the piece of paper <code>b</code> the address to a home with a sign saying <code>5</code> above the front door. Think of <code>a = b</code> as <em>copying</em> the address that is written on <code>b</code> to <code>a</code>. And then, think of <code>b.Value = 2</code> as changing the value on the sign above the front door, in this case, changing the <code>5</code> to <code>2</code>. Now, if someone asks, what is the value above the door on the home that has the address that is written on the piece of paper <code>a</code>? Well, the address on <code>a</code> is the same as the address on <code>b</code>. We just changed the value on the sign above the front door from <code>5</code> to <code>2</code>. So, we expect to see <code>2</code>.</p> <p>Try it, try it, and you will see the value <code>2</code> printed to the console.</p> <p>Think about this over and over and over until you feel it deep in your bones. Until you grasp this fundamental concept, you will find reasoning about programming to be quite challenging.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload