-1

I would like to know what happens when we create an object "a", then make a reference "b" to it and then we create a new object with "a", what happens to 56? How I understand it that "a" loses its reference to 56 creating a new reference to 20. Making "b" the only holder of reference to 56.

class SingleDigit
{
    public int Digit { get; set; }
    public SingleDigit(int b)
    {
        Digit = b;
    }
}

SingleDigit a = new SingleDigit(56); // creat new object with int 56
SingleDigit b = a; // make a reference to a
b.Digit -= 20; // both a and b's digit is subtracted
a = new SingleDigit(20); // What happens here ? Does a lose its reference to 56 ?
Sach
  • 10,091
  • 8
  • 47
  • 84
yosu
  • 33
  • 2
  • So, your main question is why b is now 36 and a is 20? – z3nth10n Aug 25 '17 at 17:25
  • 1
    After running this code, `a.Digit` is `20` and `b.Digit` is `36`, with `a` and `b` pointing to separate `SingleDigit` instances. If you are confuzzled by such things, I recommend just trying them out in LINQPad. – Jeroen Mostert Aug 25 '17 at 17:25
  • You can see this here: http://rextester.com/PAFTC79188 – z3nth10n Aug 25 '17 at 17:26
  • `SingleDigit b = a` does not *make* a reference to `a` -- `a` is a variable holding a reference to an object, and now `b` holds the same reference. You never deal with the objects directly. If `SingleDigit` was a `struct`, it *would* get copied. – Jeroen Mostert Aug 25 '17 at 17:27
  • "a" is just a variable that gets assigned a new Object. If you hadn't saved that data into the variable "b," the old data would just get Garbage Collected because there is no purpose for the data in that memory block. "a" does lose it's reference because now it is pointing to a completely different area in memory holding that new object. – A.Sharma Aug 25 '17 at 17:28
  • You should take a look here also: https://stackoverflow.com/questions/21116554/proper-way-to-implement-icloneable – z3nth10n Aug 25 '17 at 17:29

2 Answers2

2
    //Create a reference location for variable a. Assign it value 56.
    SingleDigit a = new SingleDigit(56); 

    // Create a new object b and give it reference of a's object location. 
    //So, b.Digit is also 56.
    SingleDigit b = a; 

    //Decrement 20 from b.Digit. 
    //It decrements from a.Digit as well since both 
     //refer to same memory object. So, both become 36.
    b.Digit -= 20; // both a and b's digit is subtracted

    // b holds up to original memory location. 
    //A new memory location has a new object created and 
    //its location is provided to variable a. 
    //a.Digit is now 20, and not 36.
    a = new SingleDigit(20); //a loses its reference to 36
Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22
1

Please refer to excellent resources mentioned in comments !

So that you can visualize what's happening (an oversimplified view off course) have a look at the image below :

enter image description here

At first, both a and b refer to same memory location and value they refer to is 56.

When you subtract 20 the value at this particular memory location becomes 36.

Finally when you do

a = new SingleDigit(20); 

a starts pointing to a new memory locations which contains value 20

Ankit
  • 5,733
  • 2
  • 22
  • 23
  • There is no copying whatsoever going on in this scenario, whether shallow or deep. Assigning references should not be called copying at all, to prevent confusion. – Jeroen Mostert Aug 25 '17 at 17:36
  • as i said this is an oversimplified description to visually support undesratnding ! i'll improve the language :) – Ankit Aug 25 '17 at 17:37