2

If not what's the best way to create it ?

Note: merging is not just appending, it fusionned keys that are the same.

user310291
  • 36,946
  • 82
  • 271
  • 487

4 Answers4

4

You can use the LINQ Concat() method:

using System.Linq;

// ...

var arr1 = new[] { 1, 2, 3 };
var arr2 = new[] { 4, 5, 6 };

var merged = arr1.Concat(arr2);    // This returns an IEnumerable<int>

// If you want an actual array, you can use:
var mergedArray = merged.ToArray();
Cameron
  • 96,106
  • 25
  • 196
  • 225
  • Union is a set operation. It does more than just append the sources. `new[]{1,2,3}.Union(new[]{2,3,4})` gives "1,2,3,4", instead of the expected "1,2,3,2,3,4". Furthermore, it does not guarantee order. – R. Martinho Fernandes Mar 22 '11 at 18:18
  • I didn't down vote, but Union uses the equality operator to create a single set from the results of the two arrays. array_merge when working on numerical arrays will include append all the results including duplicates without testing for equality. – Gilles Mar 22 '11 at 18:20
  • @Martinho: Oops, thanks for that. I'll change my answer to use `Concat()` – Cameron Mar 22 '11 at 18:21
  • If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. Source: http://php.net/manual/en/function.array-merge.php – WorldIsRound Mar 22 '11 at 18:21
  • @HelloWorld: Right, thanks. I've changed my answer to use Concat; in this case, there can never be string keys since that would be a dictionary in C#, not an array – Cameron Mar 22 '11 at 18:25
  • Thanks there seems to be 2 good solutions, I haven't tried yet at the moment I have checked first one as best solution but maybe I'll change my mind later after testing :) – user310291 Mar 23 '11 at 07:11
4

This functionality exist on a List element. Arrays are fixed width items in C#, so you can't modify the size without creating a new array. However, Lists are a different story. You can do:

List<int> sample = oldList.AddRange(someOtherList);
// sample contains oldList with all elements of someOtherList appended to it.

Additionally, with LINQ it's trivially easy to convert between List and Array with the

.ToList()
.ToArray()

extension methods. If you want to do that with an indeterminate number of arrays, you could do something like this:

public static class ArrayExtensions
{
     public static T[] MergeArrays<T>(this T[] sourceArray, params T[][] additionalArrays)
     {
          List<int> elements = sourceArray.ToList();

          if(additionalArrays != null)
          {
               foreach(var array in additionalArrays)
                   elements.AddRange(array.ToList());
          }

          return elements.ToArray();
     }
}

And call:

int[] mergedArray = initialArray.MergeArrays(array1, array2, array3 /* etc */);
Tejs
  • 40,736
  • 10
  • 68
  • 86
0

Duplicate. Already discussed here:

Most efficient way to append arrays in C#?

You can't merge arrays as arrays have fixed size in C#. There are numerous ways to do this with enumerables and List.

Community
  • 1
  • 1
Ade Miller
  • 13,575
  • 1
  • 42
  • 75
0

No not directly equivalent to array_merge.

If you just want to merge two arrays there is already another question : Merging two arrays in .NET

If your looking for a direct array merge replacement there is none.

Community
  • 1
  • 1
Gilles
  • 5,269
  • 4
  • 34
  • 66