Has my object copied memory or a pointer to the original memory of the object?

Sorry, if the question seems strange, I did not know how to really do this. So I'm not sure if this question has been asked before.

Take this piece of code:

Object obj = new Object(); Object obj2; obj2 = obj; 

So my question is:

When I assign obj to obj2 , does obj2 point to obj memory, or does the runtime allocate a new memory block that is identical to obj memory?

Thanks Ro.

0
source share
2 answers

obj2 has a reference to the same object that obj points to. Since they point to the same object, changes to obj2 are reflected in obj .

+2
source

Here is a simple example to illustrate that this is a link, not a copy

  public class ClassObject { public int entier; public ClassObject(int p_Initial) { this.entier = p_Initial; } } ClassObject obj1 = new ClassObject(2); Console.WriteLine(obj1.entier); ==> Console obj1.entier = 2 ClassObject obj2 = obj1; obj2.entier = 5; Console.WriteLine(obj1.entier); ==> Console obj1.entier = 5 Console.WriteLine(obj2.entier); ==> Console obj2.entier = 5 
+1
source

Source: https://habr.com/ru/post/1500794/


All Articles