I would write the loop as shomething like this:
var my2darray = new[,]
{
{ true, false, false, true },
{ true, false, false, true },
{ true, false, false, true },
{ true, false, false, true },
{ true, false, false, true },
};
var iLength = my2darray.GetLength(1);
var jLength = my2darray.GetLength(0);
for (int i = 0; i < iLength; i++)
{
for (int j = 0; j < jLength; j++)
{
file.Write("{0}\t", my2darray[j, i]);
}
file.WriteLine();
}
Be aware that when you use my2darray.GetLength(x) then your array is of type arr[,] and not arr[][].
Also notice that if you want the matrix transposed in the csv-file you'll have to change i and y: my2darray[j, i]
A jagged version could be something like this (anticipating that the j-dimension i equal throughaout the outer array (i-dimension):
var my2darray = new[]
{
new [] { true, false, false, true },
new [] { true, false, false, true },
new [] { true, false, false, true },
new [] { true, false, false, true },
new [] { true, false, false, true },
};
var iLength = my2darray.Length;
var jLength = my2darray[0].Length;
for (int j = 0; j < jLength; j++)
{
for (int i = 0; i < iLength; i++)
{
Console.Write("{0}\t", my2darray[i][j]);
}
Console.WriteLine();
}
\r\n(Windows line ending) rather than just\n. To do line endings properly regardless of platform, you can useEnvironment.NewLine.my2darraya rectangular array? That's what's implied by usingGetLength(0)andGetLength(1), but then you access its contents like it's a jagged array (my2darray[i][j]rather thanmy2darray[i,j]). Also, I agree with @Kevin that it looks like you're not being consistent about the order of your array indexers.