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#?

4
  • 3
    You are just creating references, and all of them would be pointing to a single object. Commented Jan 9, 2015 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. Commented Jan 9, 2015 at 19:58
  • See: stackoverflow.com/questions/18229463/reference-type-in-c-sharp Commented Jan 9, 2015 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 Commented Jan 9, 2015 at 21:08

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

12 Comments

Then how come when i do c = new car("something"); then the c1,c2,c3,c4,c5 doesnt change modelname ?
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.
@Shiro Because you mutated the value of the variable, you didn't mutate the object that all 5 references pointed to.
so there will be two cars both named c ? the "old" and the "new" one ?
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.
|
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#?.

Comments

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.

3 Comments

copies the reference (in other words address) Reference isn't address.
@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: stackoverflow.com/questions/430112/…
No, it isn't it. Reference - just a reference, no more, anything beyond this comes from the evil (i.e. implementation detail) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.