When is the C # / value of an object copied and when is its reference copied?

I keep repeating the same problem when the object I want to reference is copied or where the object I want to copy is referenced. This happens when I use the = operator.

For example, if I submit an object to another form, that is:

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

... and then change the object in the form, the original object will not be changed. It is as if the object was copied and not referenced. However, when I do this:

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

... and then change anotherObject , myObject will also change.

The most aggravating case is when I try to clone one of my specific 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 referenced, and any changes to obj2 changed by obj1 .

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

My question is that we do not take into account the use of the ref keyword in functions when the object is copied and when the object receives a link in each case of the question (i.e., when switching to functions during configuration, like the other objects (for example, the first two above example) when copying member variables such as third example etc.)?

+50
reference c # copy
Dec 03 '10 at 16:58
source share
3 answers

It is difficult to answer this question accurately, without spending a lot of time carefully presenting your words.

I have done this in several articles that may come in handy:

This does not mean that the articles are perfect, of course - far from them, but I tried to be as clear as I could.

I think one important thing is to separate the two concepts (passing parameters and references to value types) in your head.

To look at specific examples:

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

This means that myForm.formObject and myObject refer to the same SomeObject instance - as two people having separate sheets of paper, each of them having the same address as on them. If you go to the address on one sheet of paper and draw a house in red, then go to the address on the second sheet of paper, you will see a red house.

It is unclear what you mean by "and then modify the object in the form", because the type you provided is immutable. There is no way to change the object itself. You can change myForm.formObject to refer to another instance of SomeObject , but it's like writing an address on one sheet of paper and writing another address on it. This will not change what is written on another piece of paper.

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

+38
Dec 03 '10 at 17:16
source share

Hi Mike All objects that come from ValueType, such as structural or other primitive types, are value types. This means that they are copied whenever you assign them to a variable or pass them as a parameter to a method. Other types are reference types, which means that when assigning the reference type to a variable, not in the value, but its address in memory is assigned to the variable. You should also note that you can pass a value type as a reference using the ref keyword. Here is the syntax

 public void MyMethod(ref int a) { a = 25 } int i = 20; MyMethod(ref i); //Now i get updated to 25. 

Hope this helps :)

+4
Dec 03 '10 at 17:03
source share

Regarding the cloning of your objects, if the values ​​that you copy from one object to another are reference types, then any modification of these values ​​in the original object will affect the values ​​in the copied object (since they are just references to the same object)

If you need to clone an object that has properties that are reference types, you need to either make these types cloned, or make them manually, creating new instances if necessary.

Consider using IClonable , although it is not the best imho solution.

+1
Dec 03 '10 at 17:04
source share



All Articles