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

Mat*_*son 15 .net vb.net collections

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

For Each item As Host In hostCollection1
    hostCollection2.Add(item)
Next
Run Code Online (Sandbox Code Playgroud)

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

jop*_*jop 36

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

  • 'AddRange'不是'Collection'的成员. (3认同)

Ben*_*ein 5

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

List<string> list1 = new List<string>();
list1.Add("Hello");
List<string> list2 = new List<string>(list1);
Run Code Online (Sandbox Code Playgroud)

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