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:
For Each item As Host In hostCollection1
hostCollection2.Add(item)
Next
My collections are generic collections, inherited from the base class -- Collection(Of )
You can use AddRange: hostCollection2.AddRange(hostCollection1)
.
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With