This has nothing to do with cloning; this is a link issue.
You created two objects obj1 and obj2 and put them in a list.
Now you iterate through the collection, outputting it and get the expected results.
Below are the links:
obj1, list[0] -> Demo_obj1 (100) obj2, list[1] -> Demo_obj2 (100) Output list[0] => Demo_obj1 (100) Output list[1] => Demo_obj2 (100)
Later, obj1 = obj2 , you assigned the link from obj2 to obj1 . You do not change its value or copy your object, you simply copy the link and point to another object.
So, in fact, both of them now point to the same object.
The list contains the same two links to different objects.
list[0] -> Demo_obj1 (100) obj1, obj2, list[1] -> Demo_obj2 (100)
Then you do obj2.Value = 200 , actually changing your value to 200:
list[0] -> Demo_obj1 (100) obj1, obj2, list[1] -> Demo_obj2 (200)
When you try to infer the identifiers and values of obj1 and obj2 now, you will actually infer the value of the same object (Demo_obj2).
Output obj1 => Demo_obj2 (200) Output obj2 => Demo_obj2 (200)
However, if you try to iterate through the collection, you will again get Demo_obj1 and Demo_obj2 according to the link table.
Output list[0] => Demo_obj1 (100) Ouptut list[1] => Demo_obj2 (200)
Yeldar kurmangaliyev
source share