obtaining number of rows and columns in array

  • Thread starter Thread starter Andrus
  • Start date Start date
A

Andrus

I need to create method with signature

public IEnumerable<IDictionary> GenerateData(object[][] result) {
}

how to determine number of columns and rows in passed result array ?

result.Lenght returns total number of elements.

Andrus.
 
Hi,
As I know You can create multidimensional array in multiple ways.

If You use the typename[][] notation then You create array of arrays.
Example :
object[][] a = new object[7][];
for (int i = 0; i < 7; i++)
a = new object[i+1];
In this case a.Length will be 7. Also for example a[5].Length will be 6.

If You use the typename[,] notation You create a rectangular array.
Example:
object[,] b = new object[6, 8];
for (int i = 0; i < b.Rank; i++)
Console.WriteLine(b.GetLength(i));

In this case the dimensions(rank) of the array is returned by the Rank
property, and the length of each dimension can be get by the GetLength
method. In this case the Length property will return the product of the
lenghts of the dimensions (48 in the above example).

Hope You find this useful.
-Zsolt
 
Andrus said:
I need to create method with signature

public IEnumerable<IDictionary> GenerateData(object[][] result) {
}

how to determine number of columns and rows in passed result array ?

result.Lenght returns total number of elements.

No it does not. It returns the left most dimension.

See code below for example and hint for how to solve it.

Arne

==============================================

using System;

namespace E
{
public class Program
{
public static void Dump(object[][] result)
{
Console.WriteLine("outer: " + result.Length);
foreach(object[] resultslice in result)
{
Console.WriteLine(" inner: " + resultslice.Length);
}
}
public static void Main(string[] args)
{
string[][] a1 = new string[][] { new string[] { "A", "B",
"C" }, new string[] { "X", "Y", "Z" } };
Dump(a1);
string[][] a2 = new string[][] { new string[] { "A", "B",
"C" }, new string[] { "X", "Y" }, new string[] { "1"} };
Dump(a2);
Console.ReadKey();
}
}
}
 
Back
Top