jagged array in c# code example
Example 1: how t declare a jagged array c#
data_type[][] name_of_array = new data_type[rows][]
Example 2: jagged array java
int[][] numbers = new int[7][];
numbers[0] = new int[5];
numbers[1] = new int[11];
Example 3: how to declare 2d array in c#
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
Example 4: jagged array to 2d array c#
static T[,] To2D<T>(T[][] source)
{
try
{
int FirstDim = source.Length;
int SecondDim = source.GroupBy(row => row.Length).Single().Key;
var result = new T[FirstDim, SecondDim];
for (int i = 0; i < FirstDim; ++i)
for (int j = 0; j < SecondDim; ++j)
result[i, j] = source[i][j];
return result;
}
catch (InvalidOperationException)
{
throw new InvalidOperationException("The given jagged array is not rectangular.");
}
}