Search This Blog

April 9, 2008

How to Merge Two Arrays Using C#?

Today I went to the following forum and found someone was asking the question about how to merge two arrays using C#.

http://forums.asp.net/t/1229678.aspx

This question is interesting. Therefore, I spent some time writing source codes using C# as follows. If you want to merge 2 arrays and meet your need, you may try to extend the function.

static public void MergeMultiDimensionArray()
{

int i, j,k,m,p,q;
int[,] array1 = { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] array2 = { { 7, 8, 9 }, { 10, 11, 12 } };
int[,] mergeArray = new int[array1.GetLength(0) + array2.GetLength(0), array2.GetLength(1)];

for (i = 0; i < array1.GetLength(0); i++)
{
for (j = 0; j < array1.GetLength(1); j++)
{
mergeArray[i,j] = array1[i,j];
}

}

for (k = 0; k < array2.GetLength(0); k++)
{
for (m = 0;m< array2.GetLength(1); m++)
{
mergeArray[i, m] = array2[k, m];


}

i++;

}

for (p = 0; p < mergeArray.GetLength(0); p++)
{
for (q = 0; q < mergeArray.GetLength(1); q++)
{
Console.WriteLine(mergeArray[p, q]);
}

}


}

No comments: