Code Debugging Techniques

Debugging plays an important role in the process of creating software, which is to allow us to follow the implementation of our program step by step. With this technique we can follow the values of the local variables, because they are changing during the execution of the program, and to remove eventual errors (bugs). The process of debugging includes:

  • Finding the problems (bugs).
  • Locating the code, which causes the problems.
  • Correcting the code, which causes the problems, so that the program works correctly.
  • Testing to make sure that the program works correctly after the corrections we have made.

Debugging in Visual Studio

Visual Studio gives us a built in debugger, thanks to which we can place breakpoints at places we have chosen. When it reaches a breakpoint, the program stops running and allows step-by-step running of the remaining rows. Debugging allows us to get in the details of the program and see where exactly do the errors occur and what is the reason for this.

In order to demonstrate the debugger, we will use the following program:

static void Main(string[] args)
{
    for (int i = 0; i < 100; i++)
    {
        Console.WriteLine(i);
    }
}

We will place a breakpoint at the function Console.WriteLine(…). For this we will need to move our cursor to the row, which prints on the console, and press [F9]. A breakpoint appears, showing where the program will stop running:

Starting the Program in Debug Mode

In order to start the program in debug mode, we choose [Debug] -> [Start Debugging] or press [F5]:

After starting the program, we can see that it stops executing at line 11, where we placed our breakpoint. The code in the current row is colored in yellow and we can run it step by step. In order to go to the next row we use the key [F10]. We can see that the code on the current row hasn't executed yet. It executes when we go on the next row:

Debugger

From the Locals window we can observe the changes in the local variables. In order to open the window you must choose [Debug] -> [Windows] -> [Locals].

Debugger

results matching ""

    No results matching ""