Search code examples
c#arraysvisual-studiodebuggingindexoutofboundsexception

Visual Studio C# view which index throws System.IndexOutOfRangeException


So I have code that loops through a 2D array and assigns it a value from a 1D array in my C# program in Visual Studio. When I execute it I get an "index was outside the bounds of array" error but I have no idea which of the two arrays, or index to the arrays is throwing the error. Because I have a 2D array and a 1D array I have a total of 3 indices I am looping through in a nested loop that could be causing the error. Is it possible in visual studio to see exactly which index I am accessing incorrectly and is throwing the error?

Right now I just have the error message on this line

 2DArray[i,j] = 1DArray[k];

Solution

  • The quickest two ways would be to use debug stop or to add a watch to each integer "i,j, and k" and when the error breaks/pauses the execution you can also hover over any variable in your code to see values of it as well.

    enter image description here