1

I want to join arrays independently of their types.

T[] arr1 = new T[];
T[] arr2 new T[];
T[] newArr = Helper.GetJoint(arr1, arr2);
Yarl
  • 728
  • 1
  • 7
  • 26

1 Answers1

4

You can use LINQ for that

 T[] newArr = arr1.Concat(arr2).ToArray();

For larger arrays, where you want the allocation to be optimized, you can use the following extension method

public static T[] Append<T>(this ICollection<T> arr1, ICollection<T> arr2) {
    var newArr = new T[arr1.Count + arr2.Count];
    arr1.CopyTo(newArr, 0);
    arr2.CopyTo(newArr, arr1.Count);
    return newArr;
}

This can be called below

var newArr = arr1.Append(arr2);
Vikhram
  • 4,294
  • 1
  • 20
  • 32