Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a C# value/object copied and when is its reference copied?

I keep getting the same issue over and over again where an object I want to reference is copied or where an object I want to copy is referenced. This happens when I use the = operator.

For example, if I am sending the object to another form, ie:

SomeForm myForm = new SomeForm(); SomeObject myObject = new SomeObject(); myForm.formObject = myObject; 

...and then modify the object in the form, the original object does not get modified. It is as if the object was copied and not referenced. Yet, when I do this:

SomeObject myObject = new SomeObject(); SomeObject anotherObject = new SomeObject(); anotherObject = myObject; 

...and then modify anotherObject, myObject gets modified as well.

The most aggravating case is when I try to Clone one of my defined objects:

public class SomeObject {     double value1, value2;      //default constructor here      public SomeObject(val1, val2)     {         value1 = val1;         value2 = val2;     }      public void Clone(SomeObject thingToCopy)     {         this.value1 = thingToCopy.value1;         this.value2 = thingToCopy.value2;     } } 

when I do this...

SomeObject obj1 = new SomeObject(1, 2); SomeObject obj2 = new SomeObject(); obj2.Clone(obj1); 

...obj1 is referenced and any modifications to obj2 changes obj1.

System objects such as int, double, string, etc seem to always be copied, except for in the case of the clone method above.

My question is, not taking into account the use of the ref keyword in functions, when does an object get copied and when does an object get referenced in every case of the matter (i.e. when passing to functions, when setting as other objects (like the first two above examples), when copying member variables like the third example, etc.)?

like image 775
Mike Webb Avatar asked Dec 03 '10 16:12

Mike Webb


People also ask

Why is there a slash in AC?

Senior Member. You read it as "ei-cee" (no "slash" pronounced). In terms of distinguishing between "air conditioning" and "air conditioner," I can think of an example like "Today, I bought a new air conditioner" ("conditioning" not allowed). I personally would not say "Today, I bought a new AC."

Is AC for winter and summer?

While most of us believe that an air conditioner only works to cool a room down, that's not entirely the case. Modern air conditioners can regulate the temperature according to your liking, both in summers and winters.

What does AC stand for in weather?

AC. 1. Abbreviation for Altocumulus - a cloud of a class characterized by globular masses or rolls in layers or patches, the individual elements being larger and darker than those of cirrocumulus and smaller than those of stratocumulus. These clouds are of medium altitude, about 8000-20,000 ft (2400-6100 m). 2.

Why is AC temperature 16?

Even if the temperature indicator on the AC shows 16 degrees when it is switched on, Kumar explains, the room temperature may still be at 25 degree Celsius. The indicated temperature only means that when the temperature of the room is pulled down to 16 degrees, the motor of the AC compressor will switch off.


1 Answers

It's hard to answer this sort of question precisely without spending an awful lot of time picking your words carefully.

I've done so in a couple of articles which you may find useful:

  • Parameter passing in C# / .NET
  • Reference types and value types in C# / .NET

That's not to say that the articles are perfect, of course - far from it - but I've tried to be as clear as I can.

I think one important thing is to separate the two concepts (parameter passing and reference vs value types) out in your head.

To look at your specific examples:

SomeForm myForm = new SomeForm(); SomeObject myObject = new SomeObject(); myForm.formObject = myObject; 

This means that myForm.formObject and myObject refer to the same instance of SomeObject - like two people having separate pieces of paper, with each one having the same address written on them. If you go to the address on one piece of paper and paint the house red, then go to the address on the second piece of paper, you'll see a red house.

It's not clear what you mean by "and then modify the object in the form" because the type you have provided is immutable. There's no way of modifying the object itself. You can change myForm.formObject to refer to a different instance of SomeObject, but that's like scribbling out the address on one piece of paper and writing a different address on it instead. That won't change what's written on the other piece of paper.

If you could provide a short but complete program whose behaviour you don't understand (ideally a console application, just to keep things shorter and simpler) it would be easier to talk about things in concrete terms.

like image 165
Jon Skeet Avatar answered Sep 21 '22 17:09

Jon Skeet