My understanding of the situation is that the compiler won't accept:
test = {foo, bar};
because at compile time, it does not know the length of foo and bar,
and a multi-dimensional array must contain arrays of the same length.
That's why:
test = {{1,2,3,4,5},{6,7,8,9,10}};
works - it knows at compile time that both arrays are of length=5.
I believe you'll need your own function that joins arrays. You can
try something like this - kind of crazy-looking, but I believe it will
work:
static T[,] JoinArrays<T>(params T[][] ts)
{
if ((ts == null) || (ts.Length == 0) || (ts[0] == null))
{
return null;
}
int numArraysToJoin = ts.Length;
int numElemsInEachArray = ts[0].Length;
if (!Array.TrueForAll(ts, delegate(T[] tArray) { return ((tArray !=
null) && (tArray.Length == numElemsInEachArray)); }))
{
throw new Exception("Arrays must be the same length");
}
T[,] result = new T[numArraysToJoin, numElemsInEachArray];
for (int i = 0; i < numElemsInEachArray; i++)
{
for (int j = 0; j < numArraysToJoin; j++)
{
result[j, i] = ts[j][i];
}
}
return result;
}
On Feb 13, 11:01 am, cityuk <[email protected]> wrote:
> Dear All,
>
> I want to do something like the following and C# refuses to compile it
> - searched the web, but did not find any clue. If someone could shed
> some light, I would be grateful.
>
> int[,] test=new int[2,5];
> int[] foo = new int[5]{12,24,36,48,60};
> int[] bar = new int[5]{12,24,36,48,60};
> test ={foo, bar}; // this does not compile
> test = {{12,24,36,48,60}, {12,24,36,48,60}}; // this compiles fine and
> in my knowledge it is the same as the one on the previous line
> test = { new int[5] {12,24,36,48,60}, new int[5] {12,24,36,48,60}}; //
> does not compile again.
>
> Regards,
> George