In C#, a generic list is a data structure that can hold a group of elements of a specific type. It works as a ArrayList class, which is non-generic and can hold elements of any type.
We can use the following code to create a generic list in C#:
List<T> myList = new List<T>();
The T is the type of element the list can hold. If we want to add the list of integers we simply replace T with int.
List<int> myList = new List<int>();
A generic clone in C# is a technique for making a deep copy of a generic object. A deep copy means the object's fields and properties are also cloned, so the new object is completely separate from the original object. There is no built-in method in C# to perform a deep copy of a generic object. This is why it is important to implement the ICloneable interface and write custom code to create a deep copy of the object.
Let's try to understand with an example. In the following code example, we create a deep copy of a list of cars and demonstrate the preservation of object values between the original and cloned lists.
using System;using System.Collections.Generic;class Car : ICloneable{public string Make { get; set; }public List<string> Variants { get; set; }public Car(string make, List<string> variants){Make = make;Variants = variants;}public object Clone(){// Perform a deep copy of the Car objectList<string> clonedVariants = new List<string>(Variants);return new Car(Make, clonedVariants);}}class Program{static void Main(){// Create the original list of Car objectsList<Car> originalList = new List<Car>();originalList.Add(new Car("Toyota", new List<string> { "Camry", "Corolla" }));originalList.Add(new Car("Honda", new List<string> { "Accord", "Civic" }));originalList.Add(new Car("Hyundai", new List<string> { "Sonata", "Elantra" }));// Create a deep copy of the listList<Car> clonedList = new List<Car>();foreach (Car car in originalList){clonedList.Add((Car)car.Clone());}// Modify the original listoriginalList[0].Make = "Toyota";originalList[0].Variants[0] = "Camry 1";// Display the original and cloned listsConsole.WriteLine("Original List:");foreach (Car car in originalList){Console.WriteLine("{0}:", car.Make);foreach (string variant in car.Variants){Console.WriteLine("\t{0}", variant);}}Console.WriteLine("Cloned List:");foreach (Car car in clonedList){Console.WriteLine("{0}:", car.Make);foreach (string variant in car.Variants){Console.WriteLine("\t{0}", variant);}}}}
Lines 1—2: We add two namespace libraries System and System.Collections.Generic.
Line 4: This specifies the ICloneable interface-implementing Car class.
Lines 15–20: We create a method called Clone which will make a new List, clonedVariants to create a deep copy. This method will copy the original List to the new List.
Lines 28–31: We instantiate the List<Car> and add three Car objects using constructors.
Lines 34–38: We call the Clone method to create a deep copy of each Car object in the originalList to add it in the clonedList using Add method.
Lines 41–42: We change the data of the originalList to test if we have successfully created a deep copy.
Lines 45–53: We print the values in originalList.
Lines 55–63: We print the values in clonedList.
Free Resources