[C#] Getting started with debugging in Visual Studio 2017
For running the code in Visual Studio, we often press the keyboard combo [Ctrl] - [F5] which is "Start Without Debugging". Beginners often find it baffling because they do not know what debugging is. Well, as they advance in their programming, they will come to know debugging as a tool to help trace the program flow. In this article (the third of my Getting-Started-With-C# series hehe), I shall discuss on how to quick start on this.
Simply put, debugging is about visualizing the code as it run and this could help us locate the cause of logical errors. Recall any particular instance whereby you need to go through step by step to locate the spanner in the works, that's debugging.
I think a good problem to work with for debugging is one that ideally uses recursion. Ah ha, recursion is often the bugbear of programmers because it is usually something that either we get it or we don't (it's like the football or basketball player flair). Also, the nested function calls could really go deep and make manual pen and paper tracing tedious. OK let's get down to business, shall we.
I am using the example of Fibonacci numbers. It is ideal for recursion; see the following code snippet.
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
int n = 8;
Console.WriteLine("Fibonacci({0}) = {1}", n, Fibonacci(n));
}
static int Fibonacci(int n)
{
if (n == 0 || n == 1)
{
return n;
}
else
{
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}
}
}
If we run the code ala [Ctrl]-[F5], we get the answer very quickly but how do we obtain the intermediate results. To do so, we must first set breakpoints.
There is a thin gray column on the left of the code (line numbers). Click on it to set a breakpoint (red dot) on a corresponding line number. In my example, I set a breakpoint on line 8. What is a breakpoint? Essentially it is a traffic red light whereby the code will suspend execution. So we will want to set breakpoints at where we want to start tracing the program flow.
Instead of [Ctrl]-[F5], we hit [F5] which is "Start Debugging". Sure enough the code did a time freeze.
The Autos window will display the values of certain variables and function return values at that particular moment while the Call Stack window will show the line of code/function being executed. We can also mouse over the variables to observe their intermediate value.
Of interest is the set of controls that will allow us to navigate through the code.
We have the Step Into (F11) command which will essentially drill down into the code execution while the Step Over (F10) command will execute the next line of code. The Step Out (Shift-F11) function is the opposite of the Step Into command. Now, for our example, we shall use the Step Into function. Every time the Step Into command is used, the debugger will go deeper into the code, leaving no stones unturned (hehe).
We will be able to observe the sequence of the code flow and at each step the variables value and the function output if any. I do admit that the debugger will require some time to learn and getting used to but it can certainly help in program tracing.
Hope this article has been helpful to you as it has been to me. Good luck in all your future debugging.
Very good article, provides a good introduction into the debugging world!