简体   繁体   English

C#中的数组连接

[英]Array concatenation in C#

  1. How do I smartly initialize an Array with two (or more) other arrays in C#? 如何在C#中使用两个(或更多)其他数组巧妙地初始化数组?

     double[] d1 = new double[5]; double[] d2 = new double[3]; double[] dTotal = new double[8]; // I need this to be {d1 then d2} 
  2. Another question: How do I concatenate C# arrays efficiently? 另一个问题:如何有效地连接C#数组?

You could use CopyTo : 你可以使用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();

你可以通过首先创建dTotal,然后只使用Array.Copy复制两个输入来使它“更好”。

You need to call Array.Copy , like this: 你需要调用Array.Copy ,如下所示:

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);
using System.Linq;

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

// Concat array1 and array2.
var result1 = array1.Concat(array2).ToArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM