你如何在 C# 中连接列表? [英] How do you concatenate Lists in C#?
问题描述
如果我有:
List<string> myList1;
List<string> myList2;
myList1 = getMeAList();
// Checked myList1, it contains 4 strings
myList2 = getMeAnotherList();
// Checked myList2, it contains 6 strings
myList1.Concat(myList2);
// Checked mylist1, it contains 4 strings... why?
我在 Visual Studio 2008 中运行了与此类似的代码,并在每次执行后设置断点.在 myList1 = getMeAList();
之后,myList1
包含四个字符串,我按下加号按钮以确保它们不都是空值.
I ran code similar to this in Visual Studio 2008 and set break points after each execution. After myList1 = getMeAList();
, myList1
contains four strings, and I pressed the plus button to make sure they weren't all nulls.
在 myList2 = getMeAnotherList();
之后,myList2
包含六个字符串,我检查以确保它们不为空...在 myList1 之后.Concat(myList2);
myList1 只包含四个字符串.这是为什么?
After myList2 = getMeAnotherList();
, myList2
contains six strings, and I checked to make sure they weren't null... After myList1.Concat(myList2);
myList1 contained only four strings. Why is that?
推荐答案
Concat
返回一个新序列 不修改原始列表.试试 myList1.AddRange(myList2)
.
Concat
returns a new sequence without modifying the original list. Try myList1.AddRange(myList2)
.
这篇关于你如何在 C# 中连接列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!