1

i need to declare 16x16 matrix blocks in a loop. Because i don't know how many blocks i must create. It changes acording to width. I have a code like this and name of the blocks must go like this: "block1, block2, block(i)" How can i declare this blocks inside of a loop.

for(int i = 0; i < width; i++)
{
    int[,] block = new int[16, 16];
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • 3
    You could create a list of matrices. Something like `List`. This way you don't need a name for each one, you just refer to `block[1]`, `block[2]`, etc.. – emsimpson92 Aug 23 '18 at 23:21
  • sorry but i didnt understand this. i need a code so i create 16x16 matrix for each i values. I will use this matrix in loops again later repeadetly. example width =10 ,i need 10 matrix 16x16 totaly. – SadViolinMan Aug 23 '18 at 23:29

1 Answers1

0

Try this:

List<int[,]> blocks = new List<int[,]>();

for(int i = 0; i < width; i++)
{
  blocks.Add(new int[16, 16]());
}

Instead of having a bunch of different matrices declared, you have a list of them. You then refer to each individual matrix by its index.

Console.WriteLine(blocks[6][2, 13]);

This would print whatever is in your 7th block matrix at index [2, 13].

Hope this helps.

emsimpson92
  • 1,779
  • 1
  • 9
  • 24