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
How to copy or clone a C# list?
Copying or cloning a C# list means creating a duplicate of the original list. There are several approaches to accomplish this, each suitable for different scenarios. The choice depends on whether you need a shallow copy or deep copy, and the target data structure.
Syntax
Following are the common syntaxes for copying a list −
// Using constructor List<T> newList = new List<T>(originalList); // Using ToList() method List<T> newList = originalList.ToList(); // Using CopyTo() method T[] array = new T[originalList.Count]; originalList.CopyTo(array);
Using List Constructor
The most straightforward way to clone a list is using the List constructor that accepts an IEnumerable parameter −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<string> originalList = new List<string> {"One", "Two", "Three", "Four"};
Console.WriteLine("Original list:");
foreach(string value in originalList) {
Console.WriteLine(value);
}
// Clone using constructor
List<string> clonedList = new List<string>(originalList);
Console.WriteLine("\nCloned list:");
foreach(string value in clonedList) {
Console.WriteLine(value);
}
// Verify they are separate objects
clonedList.Add("Five");
Console.WriteLine("\nAfter adding to cloned list:");
Console.WriteLine("Original count: " + originalList.Count);
Console.WriteLine("Cloned count: " + clonedList.Count);
}
}
The output of the above code is −
Original list: One Two Three Four Cloned list: One Two Three Four After adding to cloned list: Original count: 4 Cloned count: 5
Using ToList() Method
The LINQ extension method ToList() provides another convenient way to clone a list −
using System;
using System.Collections.Generic;
using System.Linq;
public class Demo {
public static void Main() {
List<int> numbers = new List<int> {10, 20, 30, 40, 50};
Console.WriteLine("Original list:");
Console.WriteLine(string.Join(", ", numbers));
// Clone using ToList()
List<int> clonedNumbers = numbers.ToList();
Console.WriteLine("\nCloned list:");
Console.WriteLine(string.Join(", ", clonedNumbers));
// Modify cloned list
clonedNumbers[0] = 100;
Console.WriteLine("\nAfter modifying cloned list:");
Console.WriteLine("Original: " + string.Join(", ", numbers));
Console.WriteLine("Cloned: " + string.Join(", ", clonedNumbers));
}
}
The output of the above code is −
Original list: 10, 20, 30, 40, 50 Cloned list: 10, 20, 30, 40, 50 After modifying cloned list: Original: 10, 20, 30, 40, 50 Cloned: 100, 20, 30, 40, 50
Using CopyTo() Method
The CopyTo() method copies list elements to an array. This approach is useful when you need the data in array format −
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<string> list1 = new List<string> {"One", "Two", "Three", "Four"};
Console.WriteLine("First list:");
foreach(string value in list1) {
Console.WriteLine(value);
}
string[] arr = new string[list1.Count];
list1.CopyTo(arr);
Console.WriteLine("\nAfter copy to array:");
foreach(string value in arr) {
Console.WriteLine(value);
}
// Convert back to list if needed
List<string> newList = new List<string>(arr);
Console.WriteLine("\nConverted back to list count: " + newList.Count);
}
}
The output of the above code is −
First list: One Two Three Four After copy to array: One Two Three Four Converted back to list count: 4
Comparison of Copy Methods
| Method | Return Type | Performance | Use Case |
|---|---|---|---|
| List Constructor | List<T> | Fast | Simple list cloning |
| ToList() | List<T> | Fast | LINQ operations, method chaining |
| CopyTo() | Array | Fastest | Converting to array format |
Conclusion
Copying a C# list can be accomplished using the List constructor, ToList() method, or CopyTo() method. All these methods create shallow copies, meaning they copy references for reference types. Choose the List constructor or ToList() for list-to-list copying, and CopyTo() when you need an array output.
