[ACCEPTED]-.NET: Combining two generic lists-list

Accepted answer
Score: 34

This should do the trick

List<Type> list1;
List<Type> list2;

List<Type> combined;
combined.AddRange(list1);
combined.AddRange(list2);

0

Score: 19

You can simply add the items from one list 5 to the other:

list1.AddRange(list2);

If you want to keep the lists 4 and create a new one:

List<T> combined = new List<T>(list1);
combined.AddRange(list2);

Or using LINQ methods:

List<T> combined = list1.Concat(list2).ToList();

You 3 can get a bit better performance by creating 2 a list with the correct capacity before 1 adding the items to it:

List<T> combined = new List<T>(list1.Count + list2.Count);
combined.AddRange(list1);
combined.AddRange(list2);
Score: 9

If you're using C# 3.0/.Net 3.5:

List<SomeType> list1;
List<SomeType> list2;

var list = list1.Concat(list2).ToList();

0

Score: 0

More Related questions