1

I have two objects say Object1,Object2.

The two objects have the same properties.

My code is like below

Object1.property1=Object2.property1; // Object2.property1=**x**
Object2.property1= **y**;                 

When I try to retrieve the Object1.property1 it is displaying y.

Here I Don't want to change the Object1.property1 but it is Getting Modified when Object2.property1 has changed.

My Questions is

Why my code is behaving like that or Is there any concept that i don't know in c# ?

Rand Random
  • 7,300
  • 10
  • 40
  • 88
K Kiran kumar
  • 157
  • 2
  • 2
  • 11
  • "Object1.property1 it is displaying y" Are you sure? please check it again... – Shiwanka Chathuranga Jan 24 '18 at 07:03
  • 1
    The whole class code is needed to determine what the problem is here. Pereferably also the code that creates the objects. – colosso Jan 24 '18 at 07:05
  • Possible duplicate of [Why is the original object changed after a copy, without using ref arguments?](https://stackoverflow.com/questions/26063027/why-is-the-original-object-changed-after-a-copy-without-using-ref-arguments) – Mong Zhu Jan 24 '18 at 07:20
  • [this answer by John Skeet](https://stackoverflow.com/a/10604165/5174469) is also a good one, and the question almost a duplicate – Mong Zhu Jan 24 '18 at 07:21

3 Answers3

2

It is behaving like that because you don't actually have 2 objects. You only have one.

Object1 and Object2 are simply variables. They are not objects themselves. They store a "reference" that points to the object. You can use the variables to access the object. In some point in your code, you most probably have written

Object1 = Object2;

or

Object2 = Object1;

This makes the two variables hold 2 references that refers to the same object. When you edit the object by accessing through the variable Object1, you can see the effect by accessing the object through Object2. Because they are the same object.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

Fore more details you read concept of : Deep Copy vs Shollow Copy

if you want to change that behavior than you need to make use of cloning/copy , right now you are assigning reference and that's why its changing in both the object ,

as property is byte array then you can do like this ,Array.CopyTo Method (Array, Int32)

Array1.CopyTo(Array2, 0);//

so in your case its like

Object2.property1.CopyTo(Object1.property1,0);
Object2.property1= **y**;   
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • Sorry i am not getting the Memberwiseclone() when i try to code in IDE like below object1.property.Mem ....... property is byte[] array – K Kiran kumar Jan 24 '18 at 07:17
  • @KKirankumar - updated answer ...Array.CopyTo Method (Array, Int32) that you have to use – Pranay Rana Jan 24 '18 at 07:29
  • @PranayRana Array.CopyTo Method does the trick Thank You Very much can you explain how it does ?? – K Kiran kumar Jan 24 '18 at 07:59
  • @KKirankumar - you are actually copying reference of one array to other array if you dont apply copy method, but when you apply copy you are copying data not reference , that y it worked – Pranay Rana Jan 24 '18 at 09:12
  • @KKirankumar- please do accept upvote answer ...and read about cloning and copy you will get more detail – Pranay Rana Jan 24 '18 at 09:13
0

You might have to have a look at this Microsoft doc. The concept of value and reference types variables is a very basic and very important concept of c#. Simply said: if you crate an object like this:

object2 = object1;

You dont really create a new object. You just reference the first object with the second one. If eiter of those gets edited both do at the same time since they are the same object after all.

Unfortunately there is no built in way to simply clone an object, but you coud create an overload to create a new object and pass the source object as argument tocopy all the attributes individually. if there are too many attributes you could also use reflection to procedurally copy all attributes defined in the object.

colosso
  • 2,525
  • 3
  • 25
  • 49