I was working on a c# program today and ended up having to chase a bug for quite some time.
I was trying to make a copy of an object change a few fields and send it on as well as the original
for example
Function(Object A)
{
Object B = new Object();
Object B = A;
B.foo = "bar";
Send(A);
Send(B);
}
My program started treating A and B as the same object meaning that any changes to A would also change B and vise versa.
I know that the Object A and B are both referencing the same memory.
Is there a short hand way to ensure the line Object B = A
references new memory thus creating different objects. Or is the only way to create a copy constructor in my Object and create B with Object B = new Object(A)
eg:
Object(Object a){
foo = a.foo;
...
}
Basically i just what to know more about how C# handles object references and memory allocations. Big subject i know.