Suppose there is a two-dimensional array of 5x5. We take the central element [3,3] and in each element surrounding it (ie [2,2] [2,3] ... [4,4]) we change the value. We do the same thing in these elements, the value in which we have changed, etc. How can we prevent this array from going beyond the bounds of the array?

  • 3
    Check indexes is not an option? - VladD
  • 2
    Yes, immediately after writing the question, it came to that. Thanks for the reply - Draktharon

2 answers 2

Checking the indices in a two-dimensional array can be done as follows:

int[,] array = new int[5, 7]; var w = array.GetLength(0); // вернет 5 var h = array.GetLength(1); // вернет 7 if (x >= 0 && x < w && y >= 0 && y < h) { // array[x, y] } 
     if(i>=0&&i<=array.Length-1)