How do I transfer the items contained in one List
to another in C# without using foreach
?
Using the CodeThe ForEach method of the List<T> (not IList<T> ) executes an operation for every object which is stored in the list. Normally it contains code to either read or modify every object which is in the list or to do something with list itself for every object.
forEach statement is a C# generic statement which you can use to iterate over elements of a List. Also, there is a ForEach() method that a List class implements in C#.
If you as wanting to copy some specific entries from ListA into ListB you could use LINQ to retrieve the entries from ListA Regarding your statement about schema "where the schema of two lists are different".
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.
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.
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.
You could try this:
List<Int32> copy = new List<Int32>(original);
or if you're using C# 3 and .NET 3.5, with Linq, you can do this:
List<Int32> copy = original.ToList();
I see that this answer is still getting upvotes. Well, here's a secret for ya: the above answer is still using a foreach. Please don't upvote this any further.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With