1

Lets say i have the following code where Car class has only 1 property : String modelname

Car c = new Car("toyota");
Car c1 = c;
Car c2 = c;
Car c3 = c;
Car c4 = c;
Car c5 = c;

Is this going to make a new copy of car c every time ? So there will be a new "toyota" String 5 times more in the memory ? Or the "toyota" string will be in the memory only once ?

Edit: Adding this relevant link in case you had the same question as i did, i think it helps Are arrays or lists passed by default by reference in c#?

Community
  • 1
  • 1
dimitris93
  • 4,155
  • 11
  • 50
  • 86
  • 3
    You are just creating references, and all of them would be pointing to a single object. – Habib Jan 09 '15 at 19:57
  • 6
    This is such a fundamental aspect of the language that you should research it in detail. The answers here will not provide you with the required level of understanding. – usr Jan 09 '15 at 19:58
  • See: http://stackoverflow.com/questions/18229463/reference-type-in-c-sharp – Habib Jan 09 '15 at 19:59
  • 1
    Also, C# strings are immutable and each unique string is in memory only once, even if you make a **deep** copy of your car – DrKoch Jan 09 '15 at 21:08

3 Answers3

6

No, the "toyota" string will be in memory only once, because there will only be one Car object, with 6 references pointing to it.

adv12
  • 8,443
  • 2
  • 24
  • 48
  • Then how come when i do c = new car("something"); then the c1,c2,c3,c4,c5 doesnt change modelname ? – dimitris93 Jan 09 '15 at 19:57
  • 2
    Because `c` is a variable of reference type. You're making a second `Car` and assigning it to `c`, while `c1`-`c5` still point to the original `Car`. – adv12 Jan 09 '15 at 19:58
  • 1
    @Shiro Because you mutated the value of the variable, you didn't mutate the object that all 5 references pointed to. – Servy Jan 09 '15 at 20:00
  • so there will be two cars both named c ? the "old" and the "new" one ? – dimitris93 Jan 09 '15 at 20:01
  • No. There will be two `Cars`, the first one referenced by `c1`, `c2`, `c3`, `c4`, and `c5`, and the second one referenced by `c` ("named `c`" if that's how you want to think of it). Each variable points to exactly one instance of `Car`, but one of the `Cars` is referenced by more than one variable. – adv12 Jan 09 '15 at 20:04
  • Now i completely understand it yea. I am actually not new at all in programming, especially with C#, but that part wasn't clear in my head. Do you know if java works with the same way as well ? – dimitris93 Jan 09 '15 at 20:06
  • @Shiro, yes, Java and C# behave similarly in this respect. – adv12 Jan 09 '15 at 20:07
  • @Shiro You say you are not new to programming, are you coming from C++ by any chance? If you are think of the `.` in C# to behave like `->` on a pointer in C++ (This only applies to classes, different rules apply to structs). So if you had 6 pointers and you assigned a new value to the first pointer, of course 2-6 would not have their value changed. – Scott Chamberlain Jan 09 '15 at 20:13
  • @ScottChamberlain I thought that in the line `Car c = new Car(13000, "toyota");` , the `Car c` and the `new Car(13000, "toyota")` is exactly the same thing. So when i later changed the `c` value i thought that `c1 - c5` would change as well. I got that wrong. But i quickly understood how it actually works when adv12 explained it to me – dimitris93 Jan 09 '15 at 20:18
  • What would I have to change to the line `Car c1 = c;` to make it so that whenever c values were changed , c1 values would change as well ? – dimitris93 Jan 09 '15 at 20:53
  • @Shiro, you need to understand the difference between modifying the properties of an object and creating a new one. After the initialization shown in your question, you could say `c.modelname = "foo"`, which *modifies a property of the object pointed to by c*, and all other variables pointing to that object (`c1` - `c5`) would reflect the change (i.e. `c1.modelname` would also be "foo"). But if you make a new `Car` and assign it to `c`, then `c` and `c1` would point to completely different objects, and modifying one would have no effect on the other. – adv12 Jan 09 '15 at 20:58
  • oh wow, again thanks for the quick response :) I understood your answer :) – dimitris93 Jan 09 '15 at 21:04
3

Car is a Reference type, so the answer is no. See: What is the difference between a reference type and value type in c#?.

Community
  • 1
  • 1
mservidio
  • 12,817
  • 9
  • 58
  • 84
2

Assigning a reference type is only copies the reference (in other words address) of the object into the variable. It doesn't copy the actual data since the reference type variables only hold reference values or in other words an address that indicates where the actual data lives in memory. So in this case you will have 6 reference type variable that hold a reference to the same address in the memory.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • `copies the reference (in other words address)` Reference isn't address. – Hamlet Hakobyan Jan 09 '15 at 20:01
  • 2
    @HamletHakobyan yes it is. names can be different but they are same thing.a reference hold an address just like a pointer. otherwise how could it allow you to access actual object, if it doesn't hold an address? here is a relevant question: http://stackoverflow.com/questions/430112/what-is-the-difference-between-a-c-sharp-reference-and-a-pointer – Selman Genç Jan 09 '15 at 20:09
  • 1
    No, it isn't it. Reference - just a reference, no more, anything beyond this comes from the evil (i.e. implementation detail) :) – Hamlet Hakobyan Jan 09 '15 at 20:20