2

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

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

4 Answers 4

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 */);
Sign up to request clarification or add additional context in comments.

Comments

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();

6 Comments

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.
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.
@Martinho: Oops, thanks for that. I'll change my answer to use Concat()
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: php.net/manual/en/function.array-merge.php
@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
|
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.

1 Comment

the php merge doesn't append, it merges.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.