0

I have read the Difference between a Value Type and a Reference Type, in this article, the author is saying:

Because reference types represent the address of the variable rather than the data itself, assigning a reference variable to another doesn't copy the data. Instead it creates a second copy of the reference, which refers to the same location of the heap as the original value

Based on the above quote, I expected the following code which will assigning a reference variable to another, doesn't copy the data but it is indeed copying the data as well. Would you please help me to understand this?

Class1 i1 = new Class1();
i1.Name = "Name1";
Class1 i2 = i1;
//i2.Name is "Name1"

For i2 I expected it refers to the same location of the heap as the i1 value that is right, but based on the article the data should not be copied. Also my question is not deserve to be marked as duplicate because I know about the difference between value and reference type, I just need some clarification about reference types and what's the usage of deep copy/clone/shallow, if we can simply use assignment?

  • 1
    But it doesn't copy anything. Both `i1` and `i2` are pointing to the same object – Fabjan Dec 09 '18 at 08:57
  • @Fabjan So why `i2.Name` is `Name1`. Isn't this copied from `i1`? –  Dec 09 '18 at 08:59
  • 1
    Just because someone copies your phone number from your business card to a notepad, doesn't mean that you suddenly have a twin. You don't get copied. Only the reference to you (the written phone number) was copied. – madreflection Dec 09 '18 at 09:00
  • Because it's pointing to the *same* object so the values are the same – Fabjan Dec 09 '18 at 09:00
  • It's not copied. You are using the same reference to access the same data. You simply have two different names for the same reference. – Jimi Dec 09 '18 at 09:01
  • So what's the difference between the `i1`, and `i2` now? –  Dec 09 '18 at 09:01
  • 1
    The only difference between them is the name of the reference. – Zohar Peled Dec 09 '18 at 09:02
  • I mean in which situation `if(i1 == i2)` returns false? –  Dec 09 '18 at 09:03
  • And what's the usage of deep copy/clone/shallow, if we can simply use assignment? –  Dec 09 '18 at 09:04
  • You copy or clone the objects when you need to keep the original state, while using the object in a method that changes it's state. – Zohar Peled Dec 09 '18 at 09:07
  • Best way to understand might be the following. After the creation of i2 add the line `i1.Name = "new name";` You'll see that the `Name` of i2 also chances. This is because they both point to the same object. If it was a value type i2 would've been created as a copy and `i2.Name` would not be changed. You can check what happens if you use a string and once again change the first string after you've assigned it's value to string2 –  Dec 09 '18 at 09:09
  • @ZoharPeled Thanks, but what's your mean by the original state? Could you please clarify more or provide an answer? –  Dec 09 '18 at 09:09
  • A real life example: I recently wrote a service that got a message containing an object from another service. My service needed to pass parts that object to two more services - but keep the original object intact. I had to create a copy of the original object, remove unneeded parts from it (each service needed different parts), and send to the different services. If I wouldn't copy the original object, I could not sent it to both services because I had to remove different parts for each service. – Zohar Peled Dec 09 '18 at 09:14
  • Possible duplicate of [What is the difference between a reference type and value type in c#?](https://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c) – Fabjan Dec 09 '18 at 09:16
  • @Fabjan How this could be duplicate. I know what is the difference between them, I just need more clarification about reference type's behavior. –  Dec 09 '18 at 09:21
  • @SteveCode Well, it is a question that was asked here on SO number of times already... – Fabjan Dec 09 '18 at 09:24
  • @Fabjan But it's not deserved to be marked as duplicate as I know about their difference, I just need some clarification about one of them! –  Dec 09 '18 at 09:27
  • @SteveCode Don't take it so seriously, ok I've retracted the vote but still I'd suggest to read more about the subject. There are numerous articles that shed light on this and some answer your question directly. – Fabjan Dec 09 '18 at 09:36

2 Answers2

2

For i2 I expected it refers to the same location of the heap as the i1 value that is right

Correct, they refer to the same location in memory.

but based on the article the data should not be copied.

Correct again, the data is not copied, let's get some illustration together:

             _ _ _ _
            |       |
 i1  - - - -| addr  |
            |_ _ _ _|

Now, when you do Class1 i2 = i1; you're essentially copying the reference of i1 and assigning it to i2 which you can visualise as:

             _ _ _ _
            |       |
 i1  - - - -| addr  |
            |_ _ _ _|
          /
         /               
        /     
   i2  /

hence i1.Name and i2.Name have the same name because both i1 and i2 refer to the same object in memory.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • Thanks, it make sense but my question is: what's the usage of deep copy/clone/shallow, if we can simply use assignment? –  Dec 09 '18 at 09:08
  • @SteveCode deep cloning is entirely different to what the assignment operator does. The things you've mentioned there have been discussed in the past quite a lot so instead of me duplicate answers, it'll refer you to them. https://stackoverflow.com/questions/18066429/shallow-copy-or-deep-copy , https://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy?noredirect=1&lq=1 , https://stackoverflow.com/questions/2657810/deep-copy-vs-shallow-copy?noredirect=1&lq=1 etc... – Ousmane D. Dec 09 '18 at 09:14
0

Reference is just a pointer to a object. Doing that just copies reference. If you do i2.Name = "foobar"; i1.Name will also change to foobar.

Wanton
  • 800
  • 6
  • 9