Hi all
I have a question regarding arrays in c#.
I have 2 arrays say ArrayA and ArrayB, can i combine the two into one 2 dimensional array like ArrayC[ArrayA, ArrayB]
if so how??
Thank you in advance
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Aug 5, 2008, 1:54 PM
If the arrays are of the same (or compatible) types and are of the same length, then you can combine them into a two dimensional array. However, this needs to be done manually as there is no specific method to do this to my knowledge. For example:
using System;
class Test
{
static void Main()
{
int[] ArrayA = new int[]{1,2,3};
int[] ArrayB = new int[]{4,5,6};
int[,] ArrayC = new int[2,ArrayA.Length];
for (int i = 0; i < ArrayC.GetLength(1); i++)
{
ArrayC[0,i] = ArrayA[i];
ArrayC[1,i] = ArrayB[i];
}
for (int i = 0; i < ArrayC.GetLength(0); i++)
{
for (int j = 0; j < ArrayC.GetLength(1); j++)
{
Console.WriteLine(ArrayC[i,j]);
}
}
Console.ReadLine();
}
}