How do I smartly initialize an Array with two (or more) other arrays in C#?
double[] d1 = new double[5]; double[] d2 = new double[3]; double[] dTotal = new double[8]; // I need this to be {d1 then d2}
Another question: How do I concatenate C# arrays efficiently?
问问题
19383 次
4 回答
9
您可以使用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);
于 2010-05-07T12:59:46.820 回答
6
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
.
于 2010-05-07T12:55:43.783 回答
5
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);
于 2010-05-07T12:56:16.023 回答
-1
using System.Linq;
int[] array1 = { 1, 3, 5 };
int[] array2 = { 0, 2, 4 };
// Concat array1 and array2.
var result1 = array1.Concat(array2).ToArray();
于 2013-09-20T12:34:48.653 回答