Array concatenation in C#

流过昼夜 提交于 2019-11-28 13:49:15
Julien Hoarau

You could use CopyTo:

double[] d1 = new double[5];
double[] d2 = new double[3];
double[] dTotal = new double[d1.Length + d2.Length];

d1.CopyTo(dTotal, 0);
d2.CopyTo(dTotal, d1.Length);
var dTotal = d1.Concat(d2).ToArray();

You could probably make it 'better' by creating dTotal first, and then just copying both inputs with Array.Copy.

You need to call Array.Copy, like this:

double[] d1 = new double[5];
double[] d2 = new double[3];
double[] dTotal = new double[d1.length + d2.length];

Array.Copy(d1, 0, dTotal, 0, d1.Length);
Array.Copy(d2, 0, dTotal, d1.Length, d2.Length);
Roland
using System.Linq;

int[] array1 = { 1, 3, 5 };
int[] array2 = { 0, 2, 4 };

// Concat array1 and array2.
var result1 = array1.Concat(array2).ToArray();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!