7

What's the simplest way to join one or more arrays (or ArrayLists) in Visual Basic?

I'm using .NET 3.5, if that matters much.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jimtut
  • 2,366
  • 2
  • 16
  • 37

2 Answers2

11

This is in C#, but surely you can figure it out...

int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] b = new int[] { 6, 7, 8, 9, 10 };
int[] c = a.Union(b).ToArray();

It will be more efficient if instead of calling "ToArray" after the union, if you use the IEnumerable given instead.

int[] a = new int[] { 1, 2, 3, 4, 5 };
int[] b = new int[] { 6, 7, 8, 9, 10 };
IEnumerable<int> c = a.Union(b);
TheSoftwareJedi
  • 34,421
  • 21
  • 109
  • 151
  • 5
    The OP asked to join two arrays together. Not find the union which will not keep the duplicates. I do believe concat is what they might have been looking for. – uriDium Aug 12 '10 at 14:51
  • You are correct. "Join" has several meanings, but I should have understood it better. – TheSoftwareJedi Aug 30 '10 at 13:52
4

You can take a look at this thread that's titled Merging two arrays in .NET.

Community
  • 1
  • 1
mwilliams
  • 9,946
  • 13
  • 50
  • 71
  • 5
    Uncertain as to why this was the selected answer when no answer was provided. –  Sep 02 '15 at 22:35