In object-oriented programming, a common task involves creating a copy of an object. However, it's essential to distinguish between a shallow copy and a deep copy. In this post, we'll explore deep copying through a practical example, diving into the code to see how deep copying works with a custom Point
and Line
class in C#.
Shallow vs. Deep Copy
Shallow Copy: A shallow copy of an object is a new object instance but contains references to the same objects within it. Changes made to the referenced objects in the copy reflect in the original object.
Deep Copy: A deep copy creates a new object instance and also duplicates the objects referenced by it. As a result, modifying the deep-copied object does not affect the original.
Implementing Deep Copy with Point
and Line
In our code, we have two classes, Point
and Line
, which demonstrate deep copying. Let’s break down the code.
public class Point{ public int X, Y;
public Point DeepCopy() { return new Point { X = this.X, Y = this.Y }; }
public override string ToString() { return $"({X}, {Y})"; }}
The Point
class has two integer properties, X
and Y
. It also has a DeepCopy
method, which returns a new Point
object with the same values for X
and Y
.
Line
Class
The Line
class contains two Point
properties, Start
and End
, representing the start and end points of the line. In the Line
class, we define a DeepCopy
method to create a new Line
with separate Start
and End
Point
objects.
public class Line{ public Point Start, End;
public Line DeepCopy() { return new Line { Start = this.Start.DeepCopy(), End = this.End.DeepCopy() }; }
public override string ToString() { return $"Start: {Start}, End: {End}"; }}
The DeepCopy
method in the Line
class uses the DeepCopy
method in the Point
class. This way, Start
and End
get new Point
instances, ensuring that the copied line is independent of the original.
Testing Deep Copy in Main
The Program
class demonstrates how DeepCopy
works in practice. Here, line1
is our original line with specific start and end points.
var line1 = new Line{ Start = new Point { X = 1, Y = 2 }, End = new Point { X = 3, Y = 4 }};
When we create a deep copy of line1
with line1.DeepCopy()
, we get a new Line
object, line2
. Changing the coordinates in line2
does not affect line1
, showing that both lines are independent objects.
var line2 = line1.DeepCopy(); line2.Start.X = 5; line2.Start.Y = 6;
Complete Code:
using System;
namespace Coding.Exercise { public class Point { public int X, Y;
public Point DeepCopy() { return new Point { X = this.X, Y = this.Y }; }
public override string ToString() { return $"({X}, {Y})"; } }
public class Line { public Point Start, End;
public Line DeepCopy() { return new Line { Start = this.Start.DeepCopy(), End = this.End.DeepCopy() }; }
public override string ToString() { return $"Start: {Start}, End: {End}"; } }
public class Program { public static void Main() { var line1 = new Line { Start = new Point { X = 1, Y = 2 }, End = new Point { X = 3, Y = 4 } };
var line2 = line1.DeepCopy(); line2.Start.X = 5; line2.Start.Y = 6;
Console.WriteLine("Original Line: " + line1); Console.WriteLine("Copied Line: " + line2); } } }
Output:
Original Line: Start: (1, 2), End: (3, 4)Copied Line: Start: (5, 6), End: (3, 4)
The original line (line1
) remains unchanged, while the copied line (line2
) has the modified Start
coordinates, confirming that a true deep copy was made.
Conclusion
The DeepCopy
method is essential when working with objects that reference other objects, helping to create fully independent copies. This example highlights how to implement deep copying for a class that contains references to other objects, ensuring data integrity and independence.
I hope this blog helps you understand how to use DeepCopy
effectively in C#. If you have any questions or feedback, feel free to leave a comment below!
No comments:
Post a Comment