C# Shallow Copy vs Deep Copy
Understand the difference between shallow and deep copying in C#, how to implement each, and when to choose one over the other.

In C#, copying an object can produce two different results: a shallow copy or a deep copy. The distinction matters when the object contains reference-type fields. A shallow copy creates a new top-level object, but it shares references to nested objects with the original. A deep copy creates a new object and also creates independent copies of the referenced objects, so changes to the copy do not affect the original. This article explains both approaches, shows practical implementations, and describes when to use each.
What Shallow Copy Means in C#
A shallow copy creates a new object that has the same field values as the original. For value-type fields, the values are copied directly. For reference-type fields, only the references are copied, so both the original and the copy point to the same underlying objects. In practice, modifying a nested object through the copy also changes it in the original.
Consider a simple class:
public class Address
{
public string Street { get; set; }
public string City { get; set; }
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Address Home { get; set; }
}If you copy a Person instance and only duplicate its fields, the Home reference remains shared. Any change to Home.Street through the copy is visible in the original. This is the default behavior of MemberwiseClone.
What Deep Copy Means in C#
A deep copy creates a new object graph. Each mutable reference-type field is duplicated recursively, while immutable values such as strings can continue to be shared safely. The result is an independent copy: changing a nested object in the copy does not affect the original. Deep copying is more expensive than shallow copying because it allocates new instances for nested objects and must handle cycles and shared references carefully.
Using the same Person class, a deep copy would create a new Address object for the copy. The Name string is immutable, so it can be shared, but mutable reference types like Address need to be duplicated.
Implementing Shallow Copy with MemberwiseClone
The simplest way to create a shallow copy is to use Object.MemberwiseClone. This protected method creates a new object and copies all fields by value. You can expose it from your class without writing explicit copying logic.
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Address Home { get; set; }
public Person ShallowCopy()
{
return (Person)this.MemberwiseClone();
}
}MemberwiseClone is usually fast because it copies the object's fields directly. It does not call constructors, so initialization logic that would normally run during construction is skipped. It also copies references for reference-type fields, which is why the copy shares nested objects.
Implementing Deep Copy Manually
To create a deep copy, you need to explicitly copy every mutable reference-type field. For simple object graphs, you can write a method that creates new instances of each nested object.
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Address Home { get; set; }
public Person DeepCopy()
{
return new Person
{
Name = this.Name,
Age = this.Age,
Home = this.Home == null ? null : new Address
{
Street = this.Home.Street,
City = this.Home.City
}
};
}
}This approach gives you full control and is easy to read. However, it becomes tedious when the object graph is large or changes frequently. Every time you add a field, you must update the copy method, and if you forget to copy a nested object, the copy may be only partially independent. The example also includes a null check for Home, which is important when that property is optional.
Using Serialization for Deep Copy
A common alternative is to serialize the object and then deserialize it. This creates a deep copy without writing manual copying logic. The System.Text.Json serializer can be used for this purpose. BinaryFormatter can technically do the same, but it is deprecated for security reasons and should not be used in new code.
using System.Text.Json;
public static T DeepCopy<T>(T source)
{
string json = JsonSerializer.Serialize(source);
return JsonSerializer.Deserialize<T>(json);
}Serialization works well when the graph is serializable and does not include references that should be preserved as shared, such as singletons or cached services. It also requires every type in the graph to be serializable and, with System.Text.Json, the graph must not contain cycles unless you configure reference handling.
The downside is performance: serialization tends to be slower than manual copying and requires temporary memory for the serialized representation. For a one-off copy or a low-traffic path, this overhead is often acceptable. In high-throughput code, a manual deep copy is usually better.
Performance and Memory Considerations
Shallow copying is generally cheap because it only copies the top-level object's fields. Deep copying is more expensive because it can allocate new objects throughout the graph. The cost grows with the size and depth of the object graph. If you need to copy objects frequently in a hot path, a manual deep copy method is usually faster than serialization.
Memory usage also differs. A shallow copy shares nested objects, so the total memory footprint remains low. A deep copy duplicates nested objects, increasing memory usage. If the original object graph is large and you only need a few independent copies, the extra memory may be acceptable. If you copy objects repeatedly, the allocation overhead can become a problem.
Choosing Between Shallow and Deep Copy
Use a shallow copy when you need a new top-level object but are fine with sharing nested state. This is common for immutable objects where the nested references are also immutable, or when you only need to replace top-level fields without mutating the shared nested objects.
Use a deep copy when you need to modify nested objects without affecting the original. This is typical for data transfer objects, domain entities, or configuration objects that will be mutated independently. If the object graph is simple and stable, a manual deep copy is clear and efficient. If the graph is complex or changes often, serialization reduces maintenance but adds runtime cost.
One important edge case is cyclic references. A manual deep copy must handle cycles to avoid infinite recursion. With System.Text.Json, cyclic object graphs fail by default, so you need to configure a reference-preserving option or detect cycles yourself.
Another consideration is inheritance. MemberwiseClone clones the runtime type, so it works with derived types. A manual deep copy method that constructs a base type can lose derived-specific fields. Serialization can preserve the concrete type, but only if you configure polymorphic deserialization; otherwise it may create only the declared base type.
In practice, the choice comes down to how much independence you need, how complex the object graph is, and how often the copying occurs. For a one-off copy in a low-traffic path, serialization is convenient. For performance-sensitive code, a hand-written deep copy is usually better.
Try matching browser tools
All toolsSmall utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related C# guides
All C# guidesC# Dispose vs Destructor: Key Differences Explained
c# dispose vs destructor: Understand the difference between Dispose and destructors in C#, when each runs, and how to manage unmanaged resources correctly.
C# Variable Declaration: Syntax and Practical Usage
Learn how to declare variables in C# with explicit types, var, and nullable annotations, and understand how defaults, scope, and readability affect your choices.
C# Primary Constructors for Classes: Syntax and Usage
Learn how to use C# primary constructors for classes: syntax, initialization, dependency injection, and limitations with practical examples.
C# IComparer: Custom Sorting with IComparer<T>
Learn how to implement IComparer<T> in C# to define custom sort order for arrays and lists, handle nulls, and avoid common performance pitfalls.
More in C#
All C# guidesThe guides filed next to this one in C#, in library order.