你如何在 C# 中连接列表? [英] How do you concatenate Lists in C#?

查看:30
本文介绍了你如何在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆