How do you concatenate Lists in C#? How do you concatenate Lists in C#? arrays arrays

How do you concatenate Lists in C#?


Concat returns a new sequence without modifying the original list. Try myList1.AddRange(myList2).


Try this:

myList1 = myList1.Concat(myList2).ToList();

Concat returns an IEnumerable<T> that is the two lists put together, it doesn't modify either existing list. Also, since it returns an IEnumerable, if you want to assign it to a variable that is List<T>, you'll have to call ToList() on the IEnumerable<T> that is returned.


targetList = list1.Concat(list2).ToList();

It's working fine I think so. As previously said, Concat returns a new sequence and while converting the result to List, it does the job perfectly.