C# 有没有一种简单的方法可以将一个 IList<MyType> 附加到另一个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9520291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:43:18  来源:igfitidea点击:

Is there an easy way to append one IList<MyType> to another?

c#.netc#-4.0.net-4.0

提问by Michael Goldshteyn

Here is some sample code:

下面是一些示例代码:

IList<MyType> myList1=new List<MyType>();
IList<MyType> myList2=new List<MyType>();

// Populate myList1
...
// Add contents of myList1 to myList2
myList2.Add(myList1); // Does not compile

How do I add the contents of one list to another - is there a method for this?

如何将一个列表的内容添加到另一个列表 - 有没有办法做到这一点?

采纳答案by JaredPar

There's no great built-in way to do this. Really what you want is an AddRangemethod but it doesn't exist on the IList<T>(or it's hierarchy). Defining a new extension method though for this is straight forward

没有很好的内置方法可以做到这一点。你真正想要的是一种AddRange方法,但它不存在于IList<T>(或它的层次结构)中。定义一个新的扩展方法虽然很简单

public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> enumerable) {
  foreach (var cur in enumerable) {
    collection.Add(cur);
  }
}

myList2.AddRange(myList1);

回答by phoog

If the run-time type of the second list is List<T>, you can cast to that type and use the AddRange()method.

如果第二个列表的运行时类型是List<T>,则可以强制转换为该类型并使用该AddRange()方法。

Otherwise, you have to do it yourself with a loop. Alternatively, you could use linq to create a new list containing the contents of both source lists.

否则,您必须自己使用循环来完成。或者,您可以使用 linq 创建一个包含两个源列表内容的新列表。

回答by Igal Tabachnik

If you declare both list types as the concrete Listinstead of IList, you can use the AddRangemethod:

如果将两种列表类型都声明为具体List而不是IList,则可以使用以下AddRange方法:

List<MyType> myList1=new List<MyType>();
List<MyType> myList2=new List<MyType>();

myList2.AddRange(myList1);

otherwise you could use LINQ to combine the two:

否则,您可以使用 LINQ 将两者结合起来:

using System.Linq;

IList<MyType> myList1=new List<MyType>();
IList<MyType> myList2=new List<MyType>();

var newList = myList1.Concat(myList2);

回答by Lonli-Lokli

Use Enumerablr extension,

使用 Enumerablr 扩展,

myList2=new List<MyType>(myList2.Concat(myList1))

BTW, if you do not populate myList2, you can just create it based on myLis1.

顺便说一句,如果您不填充 myList2,则可以根据 myLis1 创建它。

EDIT

编辑

I've try to research perfomance for several cases

我已经尝试研究几个案例的性能

1) AddRange via Add

1)通过添加添加范围

List2.AddRange(List1);

public static class AddRangeUtils
{
    public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> enumerable)
    {
        foreach (var cur in enumerable)
        {
            collection.Add(cur);
        }
    }
}

2) Concat

2) 连接

List2 = new List<TestClass>(List2.Concat(List1))

3) Predefined Collection Count 1

3)预定义集合计数 1

var thirdList = new List<TestClass>(List2.Count + List1.Count);
foreach (var testClass in List1)
{
   thirdList.Add(testClass);
}
foreach (var testClass in List2)
{
   thirdList.Add(testClass);
}
List2 = thirdList;

4) Predefined Collection Count 2

4)预定义集合计数 2

var thirdList = new List<TestClass>(List2.Count + List1.Count);
thirdList.AddRange(List1);
thirdList.AddRange(List2);
List2 = thirdList;

Collection's Count is the count of elements for each list, List1 and List2: And came to such results (with different collection's length)

Collection's Count 是每个列表,List1 和 List2 的元素计数:并得出这样的结果(不同集合的长度)

results for calculation

计算结果

回答by arturomonriv

I used this one line approach:

我使用了这一行方法:

Array.ForEach(ilist1.ToArray(), x => ilist2.Add(x));

Array.ForEach(ilist1.ToArray(), x => ilist2.Add(x));