Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is Shallow Copy and how it is different from Deep Copy in C#?
A shallow copy creates a new object but copies only the reference values from the original object. When the copied object contains reference types, both the original and copied objects point to the same memory locations for those inner objects.
A deep copy creates a completely independent copy of an object, including all nested objects. Changes to one object do not affect the other since they occupy separate memory spaces.
Understanding Shallow Copy
In a shallow copy, only the top-level properties are copied. If the object contains reference types like arrays or objects, only the references are copied, not the actual objects themselves.
Example - Shallow Copy with Reference Assignment
using System;
class Person {
public string Name;
public int[] Numbers;
public Person(string name, int[] numbers) {
Name = name;
Numbers = numbers;
}
}
class Program {
static void Main(string[] args) {
// Original object
Person original = new Person("John", new int[] {1, 2, 3});
// Shallow copy - only reference is copied
Person shallowCopy = original;
Console.WriteLine("Before modification:");
Console.WriteLine("Original: " + original.Name + ", Numbers: [" + string.Join(", ", original.Numbers) + "]");
Console.WriteLine("Copy: " + shallowCopy.Name + ", Numbers: [" + string.Join(", ", shallowCopy.Numbers) + "]");
// Modify the copy
shallowCopy.Name = "Jane";
shallowCopy.Numbers[0] = 99;
Console.WriteLine("\nAfter modification:");
Console.WriteLine("Original: " + original.Name + ", Numbers: [" + string.Join(", ", original.Numbers) + "]");
Console.WriteLine("Copy: " + shallowCopy.Name + ", Numbers: [" + string.Join(", ", shallowCopy.Numbers) + "]");
}
}
The output of the above code is −
Before modification: Original: John, Numbers: [1, 2, 3] Copy: John, Numbers: [1, 2, 3] After modification: Original: Jane, Numbers: [99, 2, 3] Copy: Jane, Numbers: [99, 2, 3]
Understanding Deep Copy
A deep copy creates a new object and recursively copies all nested objects, ensuring complete independence between the original and copied objects.
Example - Deep Copy using MemberwiseClone
using System;
class Person : ICloneable {
public string Name;
public int[] Numbers;
public Person(string name, int[] numbers) {
Name = name;
Numbers = numbers;
}
// Deep copy implementation
public object Clone() {
Person cloned = (Person)this.MemberwiseClone();
cloned.Numbers = new int[this.Numbers.Length];
Array.Copy(this.Numbers, cloned.Numbers, this.Numbers.Length);
return cloned;
}
}
class Program {
static void Main(string[] args) {
// Original object
Person original = new Person("John", new int[] {1, 2, 3});
// Deep copy
Person deepCopy = (Person)original.Clone();
Console.WriteLine("Before modification:");
Console.WriteLine("Original: " + original.Name + ", Numbers: [" + string.Join(", ", original.Numbers) + "]");
Console.WriteLine("Copy: " + deepCopy.Name + ", Numbers: [" + string.Join(", ", deepCopy.Numbers) + "]");
// Modify the copy
deepCopy.Name = "Jane";
deepCopy.Numbers[0] = 99;
Console.WriteLine("\nAfter modification:");
Console.WriteLine("Original: " + original.Name + ", Numbers: [" + string.Join(", ", original.Numbers) + "]");
Console.WriteLine("Copy: " + deepCopy.Name + ", Numbers: [" + string.Join(", ", deepCopy.Numbers) + "]");
}
}
The output of the above code is −
Before modification: Original: John, Numbers: [1, 2, 3] Copy: John, Numbers: [1, 2, 3] After modification: Original: John, Numbers: [1, 2, 3] Copy: Jane, Numbers: [99, 2, 3]
Comparison
| Aspect | Shallow Copy | Deep Copy |
|---|---|---|
| Memory Usage | Less memory (shares references) | More memory (creates new objects) |
| Performance | Faster | Slower |
| Independence | Objects are dependent | Objects are independent |
| Use Case | When sharing data is acceptable | When complete isolation is needed |
Conclusion
Shallow copy shares references to inner objects making changes affect both copies, while deep copy creates completely independent objects. Choose shallow copy for performance when shared references are acceptable, and deep copy when complete object independence is required.
