0

I want to copy a reference object to a new reference object, and then change values in the second reference object without the first reference object being effected. How can I do it without creating a custom constructor that takes in an object and then copy each value?

example:

class Point
    {
        public int x { get;set; } = 0;
        public int y { get; set; } = 0;
(....)
//main:
Point p1 = new Point(5, 5);
Point p2 = p1;
p2.x = 1;
// I want: p1 is (5,5) and p2 is (1,5)
// actually happends: p1 is (1,5) and p2 is (1,5)
  • Look at https://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-of-an-object-in-net-c-specifically – emert117 Sep 14 '19 at 05:57
  • Possible duplicate of [How do you do a deep copy of an object in .NET (C# specifically)?](https://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-of-an-object-in-net-c-specifically) – emert117 Sep 14 '19 at 05:57
  • 2
    Another solution is to design your X,Y point a s an immutable value type (struct with readonly properties). The cloning is then implicitly solved. – H H Sep 14 '19 at 07:28

2 Answers2

2

when we try to copy one object to another object, both the objects will share the same memory address. Normally, we use assignment operator, = , to copy the reference, not the object except when there is value type field. This operator will always copy the reference, not the actual object.

Where Shallow Copy Creating a new object and then copying the value type fields of the current object to the new object. But when the data is reference type, then the only reference is copied but not the referred object itself. Therefore the original and clone refer to the same object.

In this scenario below code may helpful:

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        Point p1 = new Point(5, 5);
        Point p2 = p1.ShallowCopy();
        p2.x = 1;

        Console.WriteLine("p1.x:{0}-------p2.x:{1}", p1.x , p2.x);
        Console.ReadKey();
    }
}

class Point
{
    public int x { get;set; } 
    public int y { get; set; }

    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    public Point ShallowCopy()
    {
        return (Point)this.MemberwiseClone();
    }
}
}

try this one.

Divyesh patel
  • 967
  • 1
  • 6
  • 21
0

What you're trying to achieve is get a deep copy of the source object, so that when you modify the clone, the source doesn't get affected.

Although there are many ways you could achieve it and I'll leave it to you to read about shallow and deep copy and the ways you can achieve it, but a quick solution could be to add a copy method to your source object inside which you can make a call to

this.MemberwiseClone();

which will return a shallow copy of the object and then if that object has any fields or properties that are reference type, you can change them to a new object by instantiating a new object of that type, once done, return that object and you should be good to go.

Hope that helps.

aghashamim
  • 553
  • 3
  • 9
  • "cannot access protected member object.memberwiseclone() via a qualifier of type 'point'; the qualifier must be of type 'program ' (or derived from it)" –  Sep 14 '19 at 06:01
  • @StackOverflow Updated the answer. – aghashamim Sep 14 '19 at 06:03