将集合项复制到 .NET 中的另一个集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/147416/
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-09-03 10:08:12  来源:igfitidea点击:

Copy collection items to another collection in .NET

.netvb.netcollections

提问by Matt Hanson

In .NET (VB), how can I take all of the items in one collection, and add them to a second collection (without losing pre-existing items in the second collection)? I'm looking for something a little more efficient than this:

在 .NET (VB) 中,如何获取一个集合中的所有项目,并将它们添加到第二个集合(而不会丢失第二个集合中预先存在的项目)?我正在寻找比这更有效的东西:

For Each item As Host In hostCollection1
    hostCollection2.Add(item)
Next

My collections are generic collections, inherited from the base class -- Collection(Of )

我的集合是泛型集合,继承自基类 -- Collection(Of )

回答by jop

You can use AddRange: hostCollection2.AddRange(hostCollection1).

您可以使用 AddRange: hostCollection2.AddRange(hostCollection1)

回答by Ben Hoffstein

I know you're asking for VB, but in C# you can just use the constructor of the collection to initialize it with any IEnumerable. For example:

我知道您要求使用 VB,但在 C# 中,您可以使用集合的构造函数来使用任何 IEnumerable 对其进行初始化。例如:

List<string> list1 = new List<string>();
list1.Add("Hello");
List<string> list2 = new List<string>(list1);

Perhaps the same kind of thing exists in VB.

也许在VB中也存在同样的事情。

回答by johnc

I always use the List<T>.AddRange(otherList<T>)function. Again, if this is a list of objects, they will be references the same thing.

我总是使用该List<T>.AddRange(otherList<T>)功能。同样,如果这是一个对象列表,它们将引用相同的东西。

You have not specified what sort of collection though, AddRange doesn't exist in CollectionBase inherited objects

您尚未指定哪种集合,但 CollectionBase 继承对象中不存在 AddRange

回答by David Robbins

Don't forget that you will be getting a reference and not a copy if you initialize your List2 to List1. You will still have one set of strings unless you do a deep clone.

不要忘记,如果您将 List2 初始化为 List1,您将获得引用而不是副本。除非您进行深度克隆,否则您仍将拥有一组字符串。

回答by Kangkan

This is available when one use an IList. But AddRangemethod is not available in Collection. I thought of casting Collectionto List, but it is not possible.

当使用IList. 但是AddRange方法在Collection. 我想投射CollectionList,但这是不可能的。

回答by Austin Salonen

Ben's solution does exist for VB.Net:

Ben 的解决方案确实存在于 VB.Net:

Dim collection As IEnumerable(Of T)    
Dim instance As New List(collection)

Here is the linked documentation.

这是链接的文档

However, one thing I would be concerned with is whether or not it does a shallow copy or a deep copy.

但是,我会关心的一件事是它是否执行浅拷贝或深拷贝。

回答by Cory Foy

Unless you want both collections to modify the same set of objects, then each object is going to have to be copied to the Heap. Maybe you can describe your scenario of how this is impacting your performance and we can find a good solution.

除非您希望两个集合都修改同一组对象,否则每个对象都必须复制到堆中。也许您可以描述一下这如何影响您的性能的场景,我们可以找到一个很好的解决方案。

回答by Cory Foy

Array.Copymay solve your problem.

Array.Copy可能会解决您的问题。

回答by Aaron Powell

回答by user4951

Not sure for this one

不确定这个

Why not just do this

为什么不这样做

    Dim newlines = _singSongs.ToList

That .tolist means it creates a whole new list

.tolist 意味着它创建了一个全新的列表