You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.6 KiB

namespace ex_BindingToA2dArray_context;
public class Matrix2d
{
private static Random random = new Random();
private static int[,] RandomInit(int nbRows, int nbColumns)
{
int[,] mat = new int[6,7];
for(int i=0; i<6; i++)
{
for(int j=0; j<7; j++)
{
mat[i,j] = random.Next(101);
}
};
return mat;
}
public Matrix2d(int nbRows, int nbColumns)
{
matrix = RandomInit(nbRows, nbColumns);
}
private readonly int[,]? matrix;
public int NbRows => matrix?.GetLength(0) ?? 0;
public int NbColumns => matrix?.GetLength(1) ?? 0;
public int[,] Matrix
{
get
{
if(matrix == null) return new int[,]{{}};
int[,] mat = new int[NbRows, NbColumns];
for(int numRow=0; numRow<NbRows; numRow++)
{
for(int numCol=0; numCol<NbColumns; numCol++)
{
mat[numRow, numCol] = matrix[numRow, numCol];
}
}
return matrix;
}
}
public IEnumerable<int> FlatMatrix2d
{
get
{
List<int> flatMatrix = new();
if(matrix == null) return flatMatrix;
int[,] mat = new int[NbRows, NbColumns];
for(int numRow=0; numRow<NbRows; numRow++)
{
for(int numCol=0; numCol<NbColumns; numCol++)
{
flatMatrix.Add(matrix[numRow, numCol]);
}
}
return flatMatrix;
}
}
}