C# List Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To deep clone a generic List<T>
in C#, you can implement a custom deep cloning method for the type T
and then use it to create a new list with cloned items. Here's an example:
ICloneable
interface:public class Person : ICloneable { public int Id { get; set; } public string Name { get; set; } public object Clone() { return new Person { Id = this.Id, Name = this.Name }; } }
In this example, the Person
class implements the ICloneable
interface and provides a Clone()
method that creates a new Person
object with the same properties as the current object.
List<T>
:public static List<T> DeepCloneList<T>(List<T> source) where T : ICloneable { return source.Select(item => (T)item.Clone()).ToList(); }
This method uses the Select()
LINQ extension method to create a new list of cloned items. It requires that the type T
implements the ICloneable
interface.
DeepCloneList()
method to deep clone a List<T>
:List<Person> people = new List<Person> { new Person { Id = 1, Name = "John" }, new Person { Id = 2, Name = "Jane" }, new Person { Id = 3, Name = "Alice" }, new Person { Id = 4, Name = "Bob" } }; List<Person> clonedPeople = DeepCloneList(people); // Modify the original list and check if the cloned list is independent people[0].Name = "Changed"; Console.WriteLine(people[0].Name); // Outputs "Changed" Console.WriteLine(clonedPeople[0].Name); // Outputs "John"
In this example, the DeepCloneList()
method is called to deep clone a List<Person>
. After modifying the original list, the cloned list remains unchanged, confirming that a deep clone was performed.
Note that this approach requires each type T
in the list to implement the ICloneable
interface and provide a custom deep cloning method. If the type T
is not under your control or does not implement ICloneable
, you might need to consider alternative methods like serialization-based cloning (e.g., using BinaryFormatter or other serialization libraries). However, using serialization for deep cloning can be slower and might not be suitable for all types.
C# deep clone List<T>:
List<int> originalList = new List<int> { 1, 2, 3 }; List<int> clonedList = new List<int>(originalList);
Clone List of objects in C#:
List<Person> originalList = new List<Person> { /* List of Person objects */ }; List<Person> clonedList = new List<Person>(originalList);
List<T> deep copy method in C#:
public static List<T> DeepCloneList<T>(List<T> original) { return new List<T>(original); }
Deep copying List<T> with serialization in C#:
List<Person> originalList = new List<Person> { /* List of Person objects */ }; List<Person> clonedList; using (MemoryStream stream = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, originalList); stream.Seek(0, SeekOrigin.Begin); clonedList = (List<Person>)formatter.Deserialize(stream); }
Deep copy List<T> using LINQ in C#:
List<Person> originalList = new List<Person> { /* List of Person objects */ }; List<Person> clonedList = originalList.Select(person => (Person)person.Clone()).ToList();
Clone List<T> with Json.NET in C#:
List<Person> originalList = new List<Person> { /* List of Person objects */ }; List<Person> clonedList = JsonConvert.DeserializeObject<List<Person>>(JsonConvert.SerializeObject(originalList));
Deep copying List<T> with AutoMapper in C#:
List<Person> originalList = new List<Person> { /* List of Person objects */ }; List<Person> clonedList = Mapper.Map<List<Person>>(originalList);
Clone List<T> using XmlSerializer in C#:
List<Person> originalList = new List<Person> { /* List of Person objects */ }; using (MemoryStream stream = new MemoryStream()) { XmlSerializer serializer = new XmlSerializer(typeof(List<Person>)); serializer.Serialize(stream, originalList); stream.Seek(0, SeekOrigin.Begin); List<Person> clonedList = (List<Person>)serializer.Deserialize(stream); }