How do you concatenate Lists in C#?

前端 未结 6 1957
闹比i
闹比i 2020-12-23 13:09

If I have:

List myList1;
List myList2;

myList1 = getMeAList();
// Checked myList1, it contains 4 strings

myList2 = getMeAnother         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-23 13:30

    Try this:

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

    Concat returns an IEnumerable 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, you'll have to call ToList() on the IEnumerable that is returned.

提交回复
热议问题