- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I copy items from list to list without foreach in C#?
The List<T> is a collection of strongly typed objects that can be accessed by index and having methods for sorting, searching, and modifying list. It is the generic version of the ArrayList that comes under System.Collection.Generic namespace.
List<T> equivalent of the ArrayList, which implements IList<T>.
It comes under System.Collection.Generic namespace.
List<T>can contain elements of the specified type. It provides compile-time type checking and doesn't perform boxing-unboxing because it is generic.
Elements can be added using the Add(), AddRange() methods or collection-initializer syntax.
Elements can be accessed by passing an index e.g. myList[0]. Indexes start from zero. List<T> performs faster and less error-prone than the ArrayList.
A list can be accessed by an index, a for/foreach loop, and using LINQ queries. Indexes of a list start from zero.
Pass an index in the square brackets to access individual list items, same as array. Use a foreach or for loop to iterate a List<T> collection.
Method 1
class Program{ public static void Main(){ List<int>originalList=new List<int>(){1,2,3,4,5,7,8,9}; List<Int32>copy = new List<Int32>(originalList); foreach (var item in copy){ System.Console.WriteLine(item); } Console.ReadLine(); } }
Output
1 2 3 4 5 7 8 9
Method 2
class Program{ public static void Main(){ List<int>originalList = new List<int>() { 1, 2, 3, 4, 5, 7, 8, 9 }; List<Int32> copy = originalList.ToList(); foreach (var item in copy){ System.Console.WriteLine(item); } Console.ReadLine(); } }
Output
1 2 3 4 5 7 8 9
Method 3
class Program{ public static void Main(){ List<int> originalList = new List<int>() { 1, 2, 3, 4, 5, 7, 8, 9 }; List<Int32> copy = originalList.GetRange(0, 3); foreach (var item in copy){ System.Console.WriteLine(item); } Console.ReadLine(); } }
Output
1 2 3
- Related Articles
- How to remove items from a list in C#?
- How do you copy an element from one list to another in Java?
- How do you remove multiple items from a list in Python?
- How do you copy a list in Java?
- How do I order a list and add position to its items in MongoDB?
- How do I insert an item between two items in a list in Java?
- How to copy a list to another list in Java?
- Java Program to copy value from one list to another list
- How to copy or clone a C# list?
- How to add items to a list in C#?
- How do I remove multiple elements from a list in Java?
- How to find items in one list that are not in another list in C#?
- How do you make a shallow copy of a list in Java?
- How do I empty a list in Java?
- How do I search a list in Java?
