# 如何在C＃中连接两个数组？

int[] x = new int [] { 1, 2, 3}; int[] y = new int [] { 4, 5 }; int[] z = // your answer here... Debug.Assert(z.SequenceEqual(new int[] { 1, 2, 3, 4, 5 }));

int[] z = x.Concat(y).ToArray();

var z = new int[x.Length + y.Length]; x.CopyTo(z, 0); y.CopyTo(z, x.Length);

List<int> list = new List<int>(); list.AddRange(x); list.AddRange(y); int[] z = list.ToArray();

public static T[] Concat<T>(this T[] x, T[] y) { if (x == null) throw new ArgumentNullException("x"); if (y == null) throw new ArgumentNullException("y"); int oldLen = x.Length; Array.Resize<T>(ref x, x.Length + y.Length); Array.Copy(y, 0, x, oldLen, y.Length); return x; }

int[] x = {1,2,3}, y = {4,5}; int[] z = x.Concat(y); // {1,2,3,4,5}

public static T[] ConcatArrays<T>(params T[][] list) { var result = new T[list.Sum(a => a.Length)]; int offset = 0; for (int x = 0; x < list.Length; x++) { list[x].CopyTo(result, offset); offset += list[x].Length; } return result; }

int[] a = new int[] { 1, 2, 3 }; int[] b = new int[] { 4, 5, 6 }; int[] c = new int[] { 7, 8 }; var y = ConcatArrays(a, b, c); //Results in int[] {1,2,3,4,5,6,7,8}

using System.Linq; int[] array1 = { 1, 3, 5 }; int[] array2 = { 0, 2, 4 }; // Concatenate array1 and array2. var result1 = array1.Concat(array2);

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace concat { class Program { static void Main(string[] args) { int[] x = new int [] { 1, 2, 3}; int[] y = new int [] { 4, 5 }; int itter = 50000; Console.WriteLine("test iterations: {0}", itter); DateTime startTest = DateTime.Now; for(int i = 0; i < itter; i++) { int[] z; z = x.Concat(y).ToArray(); } Console.WriteLine ("Concat Test Time in ticks: {0}", (DateTime.Now - startTest).Ticks ); startTest = DateTime.Now; for(int i = 0; i < itter; i++) { var vz = new int[x.Length + y.Length]; x.CopyTo(vz, 0); y.CopyTo(vz, x.Length); } Console.WriteLine ("CopyTo Test Time in ticks: {0}", (DateTime.Now - startTest).Ticks ); startTest = DateTime.Now; for(int i = 0; i < itter; i++) { List<int> list = new List<int>(); list.AddRange(x); list.AddRange(y); int[] z = list.ToArray(); } Console.WriteLine("list.AddRange Test Time in ticks: {0}", (DateTime.Now - startTest).Ticks); startTest = DateTime.Now; for (int i = 0; i < itter; i++) { int[] z = Methods.Concat(x, y); } Console.WriteLine("Concat(x, y) Test Time in ticks: {0}", (DateTime.Now - startTest).Ticks); startTest = DateTime.Now; for (int i = 0; i < itter; i++) { int[] z = Methods.ConcatArrays(x, y); } Console.WriteLine("ConcatArrays Test Time in ticks: {0}", (DateTime.Now - startTest).Ticks); startTest = DateTime.Now; for (int i = 0; i < itter; i++) { int[] z = Methods.SSConcat(x, y); } Console.WriteLine("SSConcat Test Time in ticks: {0}", (DateTime.Now - startTest).Ticks); startTest = DateTime.Now; for (int k = 0; k < itter; k++) { int[] three = new int[x.Length + y.Length]; int idx = 0; for (int i = 0; i < x.Length; i++) three[idx++] = x[i]; for (int j = 0; j < y.Length; j++) three[idx++] = y[j]; } Console.WriteLine("Roll your own Test Time in ticks: {0}", (DateTime.Now - startTest).Ticks); startTest = DateTime.Now; for (int i = 0; i < itter; i++) { int[] z = Methods.ConcatArraysLinq(x, y); } Console.WriteLine("ConcatArraysLinq Test Time in ticks: {0}", (DateTime.Now - startTest).Ticks); startTest = DateTime.Now; for (int i = 0; i < itter; i++) { int[] z = Methods.ConcatArraysLambda(x, y); } Console.WriteLine("ConcatArraysLambda Test Time in ticks: {0}", (DateTime.Now - startTest).Ticks); startTest = DateTime.Now; for (int i = 0; i < itter; i++) { List<int> targetList = new List<int>(x); targetList.Concat(y); } Console.WriteLine("targetList.Concat(y) Test Time in ticks: {0}", (DateTime.Now - startTest).Ticks); startTest = DateTime.Now; for (int i = 0; i < itter; i++) { int[] result = x.ToList().Concat(y.ToList()).ToArray(); } Console.WriteLine("x.ToList().Concat(y.ToList()).ToArray() Test Time in ticks: {0}", (DateTime.Now - startTest).Ticks); } } static class Methods { public static T[] Concat<T>(this T[] x, T[] y) { if (x == null) throw new ArgumentNullException("x"); if (y == null) throw new ArgumentNullException("y"); int oldLen = x.Length; Array.Resize<T>(ref x, x.Length + y.Length); Array.Copy(y, 0, x, oldLen, y.Length); return x; } public static T[] ConcatArrays<T>(params T[][] list) { var result = new T[list.Sum(a => a.Length)]; int offset = 0; for (int x = 0; x < list.Length; x++) { list[x].CopyTo(result, offset); offset += list[x].Length; } return result; } public static T[] SSConcat<T>(this T[] first, params T[][] arrays) { int length = first.Length; foreach (T[] array in arrays) { length += array.Length; } T[] result = new T[length]; length = first.Length; Array.Copy(first, 0, result, 0, first.Length); foreach (T[] array in arrays) { Array.Copy(array, 0, result, length, array.Length); length += array.Length; } return result; } public static T[] ConcatArraysLinq<T>(params T[][] arrays) { return (from array in arrays from arr in array select arr).ToArray(); } public static T[] ConcatArraysLambda<T>(params T[][] arrays) { return arrays.SelectMany(array => array.Select(arr => arr)).ToArray(); } } }

public static T[] Concat<T>(this T[] first, params T[][] arrays) { int length = first.Length; foreach (T[] array in arrays) { length += array.Length; } T[] result = new T[length]; length = first.Length; Array.Copy(first, 0, result, 0, first.Length); foreach (T[] array in arrays) { Array.Copy(array, 0, result, length, array.Length); length += array.Length; } return result; }

Concat方法要小心。 C＃中的数组串联解释说明：

var z = x.Concat(y).ToArray();

int[] x = new int [] { 1, 2, 3}; int[] y = new int [] { 4, 5 }; int[] z = new int[x.Length + y.Length]; var byteIndex = x.Length * sizeof(int); Buffer.BlockCopy(x, 0, z, 0, byteIndex); Buffer.BlockCopy(y, 0, z, byteIndex, y.Length * sizeof(int));

Concat花了3088ms

CopyTo花了1079ms

BlockCopy花了603ms

Concat花了45945ms

CopyTo需要2230ms

BlockCopy花了1689ms

string[] one = new string[] { "a", "b" }; string[] two = new string[] { "c", "d" }; string[] three; three = new string[one.Length + two.Length]; int idx = 0; for (int i = 0; i < one.Length; i++) three[idx++] = one[i]; for (int j = 0; j < two.Length; j++) three[idx++] = two[j];

public static class ArrayExtention { public static T[] Concatenate<T>(this T[] array1, T[] array2) { T[] result = new T[array1.Length + array2.Length]; array1.CopyTo(result, 0); array2.CopyTo(result, array1.Length); return result; } }

public static T[] ConcatArraysLinq<T>(params T[][] arrays) { return (from array in arrays from arr in array select arr).ToArray(); }

public static T[] ConcatArraysLambda<T>(params T[][] arrays) { return arrays.SelectMany(array => array.Select(arr => arr)).ToArray(); }

T[] r1 = new T[size1]; T[] r2 = new T[size2]; List<T> targetList = new List<T>(r1); targetList.Concat(r2); T[] targetArray = targetList.ToArray();

static IEnumerable<T> Merge<T>(params T[][] arrays) { var merged = arrays.SelectMany(arr => arr); foreach (var t in merged) yield return t; }

int[] x={1, 2, 3}; int[] y={4, 5, 6}; var z=Merge(x, y); // 'z' is IEnumerable<T> var za=z.ToArray(); // 'za' is int[]

static IEnumerable<T> Merge<T>(this T[] array1, T[] array2) { var merged = array1.Concat(array2); foreach (var t in merged) yield return t; }

int[] x1={1, 2, 3}; int[] x2={4, 5, 6}; int[] x3={7, 8}; var z=x1.Merge(x2).Merge(x3); // 'z' is IEnumerable<T> var za=z.ToArray(); // 'za' is int[]

using System.Linq; int firstArray = {5,4,2}; int secondArray = {3,2,1}; int[] result = firstArray.ToList().Concat(secondArray.ToList()).toArray();

array3 = array1.Union(array2).ToArray();
static class Extensions { public static T[] Concat<T>(this T[] array1, params T[] array2) { return ConcatArray(array1, array2); } public static T[] ConcatArray<T>(params T[][] arrays) { int l, i; for (l = i = 0; i < arrays.Length; l += arrays[i].Length, i++); var a = new T[l]; for (l = i = 0; i < arrays.Length; l += arrays[i].Length, i++) arrays[i].CopyTo(a, l); return a; } }

int [] x = new int [] {1，2，3}; int [] y = new int [] {4，5};

int [] z = x.Union（y）.ToArray（）;