Dot-Net
將集合項複製到 .NET 中的另一個集合
在 .NET (VB) 中,如何獲取一個集合中的所有項目,並將它們添加到第二個集合中(而不會失去第二個集合中預先存在的項目)?我正在尋找比這更有效的東西:
For Each item As Host In hostCollection1 hostCollection2.Add(item) Next我的集合是泛型集合,繼承自基類 – Collection(Of )
您可以使用 AddRange:
hostCollection2.AddRange(hostCollection1)。
我知道您要的是 VB,但在 C# 中,您可以使用集合的建構子使用任何 IEnumerable 對其進行初始化。例如:
List<string> list1 = new List<string>(); list1.Add("Hello"); List<string> list2 = new List<string>(list1);也許在 VB 中也存在同樣的事情。