Languages
[Edit]
EN

C# / .NET - copy List<> to another list

0 points
Created by:
martineau
1350

In this article, we would like to show you how to copy List to another List in C#.

Quick solution:

or:

or:

 

 

Practical example

Edit

In this example, we create copy of myList using List Constructor.

Output:

2. Using Linq ToList()

Edit

In this example, we use ToList() method from System.Linq to create a copy of myList.

Output:

3. Using getRange()

Edit

In this example, we use getRange() to copy elements from myList into a new List.

Output:

Note:

All of the presented solutions present how to create a shallow copy of the List.

References

Edit
  1. List<T>.GetRange(Int32, Int32) Method (System.Collections.Generic) | Microsoft Docs
  2. Enumerable.ToList<TSource>(IEnumerable<TSource>) Method (System.Linq) | Microsoft Docs

Alternative titles

  1. C# / .NET - clone List<> to another list
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join