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 do I copy items from list to list without foreach in C#?
There are several efficient methods to copy items from one list to another in C# without using a foreach loop. These methods provide different approaches depending on whether you need a complete copy, partial copy, or want to append items to an existing list.
Using List Constructor
The most straightforward method is to use the List<T> constructor that accepts an IEnumerable<T> parameter. This creates a new list with all elements from the source list −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
List<int> originalList = new List<int>() { 1, 2, 3, 4, 5, 7, 8, 9 };
List<int> copy = new List<int>(originalList);
Console.WriteLine("Original list copied:");
foreach (var item in copy) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Original list copied: 1 2 3 4 5 7 8 9
Using ToList() Extension Method
The ToList() extension method from LINQ creates a new list containing all elements from the source collection −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
public static void Main() {
List<int> originalList = new List<int>() { 1, 2, 3, 4, 5, 7, 8, 9 };
List<int> copy = originalList.ToList();
Console.WriteLine("List copied using ToList():");
foreach (var item in copy) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
List copied using ToList(): 1 2 3 4 5 7 8 9
Using GetRange() for Partial Copy
The GetRange() method allows you to copy a specific range of elements from the source list by specifying the starting index and count −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
List<int> originalList = new List<int>() { 1, 2, 3, 4, 5, 7, 8, 9 };
List<int> copy = originalList.GetRange(0, 3);
Console.WriteLine("First 3 elements copied using GetRange():");
foreach (var item in copy) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
First 3 elements copied using GetRange(): 1 2 3
Using AddRange() to Append Items
When you need to copy items to an existing list, use the AddRange() method to append all elements from the source list −
using System;
using System.Collections.Generic;
class Program {
public static void Main() {
List<int> originalList = new List<int>() { 1, 2, 3, 4, 5 };
List<int> targetList = new List<int>() { 10, 20 };
targetList.AddRange(originalList);
Console.WriteLine("Combined list using AddRange():");
foreach (var item in targetList) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
Combined list using AddRange(): 10 20 1 2 3 4 5
Comparison of Methods
| Method | Use Case | Performance |
|---|---|---|
| List Constructor | Complete copy to new list | Fast, single allocation |
| ToList() | LINQ operations, complete copy | Fast, requires LINQ namespace |
| GetRange() | Partial copy of elements | Fast, copies specified range only |
| AddRange() | Append to existing list | Fast, extends existing list |
Conclusion
C# provides multiple efficient methods to copy list items without using foreach loops. Use the List constructor or ToList() for complete copies, GetRange() for partial copies, and AddRange() to append items to existing lists. These methods are more concise and often more performant than manual iteration.
