合并在.NET两个数组 [英] Merging two arrays in .NET

查看:155
本文介绍了合并在.NET两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个内置的功能在.NET 2.0,将采取两个阵列,并将它们合并成一个阵列?

Is there a built in function in .NET 2.0 that will take two arrays and merge them into one array?

的阵列都是相同类型的。我从我的code基一种广泛使用的功能,让这些阵列,并不能修改该函数返回的数据以不同的格式。

The arrays are both of the same type. I'm getting these arrays from a widely used function within my code base and can't modify the function to return the data in a different format.

我在寻找,以避免写我自己的功能,如果能够实现这一点。

I'm looking to avoid writing my own function to accomplish this if possible.

推荐答案

如果你可以操纵的阵列之一,您可以在执行复制前调整其大小:

If you can manipulate one of the arrays, you can resize it before performing the copy:

T[] array1 = getOneArray();
T[] array2 = getAnotherArray();
int array1OriginalLength = array1.Length;
Array.Resize<T>(ref array1, array1OriginalLength + array2.Length);
Array.Copy(array2, 0, array1, array1OriginalLength, array2.Length);

另外,你可以做一个新的数组

Otherwise, you can make a new array

T[] array1 = getOneArray();
T[] array2 = getAnotherArray();
T[] newArray = new T[array1.Length + array2.Length];
Array.Copy(array1, newArray, 0);
Array.Copy(array2, 0, newArray, array1.Length, array2.Length);

更多关于MSDN 可用阵列的方法。

这篇关于合并在.NET两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆