How To Debug Code In Visual Studio

In early days of programming big machines were used to perform small tasks. Once upon a time that machine stopped working and the problem was caused due to a bug that died in the wiring and made the machine to stop. Since then people started calling any issue in the software as bug, and step by step process to look into the code is called debugging. In this article we will learn about how to debug our code, how to setup breakpoints and other debugging related thing.


how to debug code in visual studio





What Exactly Is Debugging?


Debugging is a process to go through the execution of the code at run-time and evaluate whether the written code is working as expected. It is useful to reach a certain line of code and further on from that line you can dig deep into your code, from one function to another, notice the change in variable values and do much more stuffs. It is really helpful when you are unable to find out why your code isn't working as per your logic.

What Is A Breakpoint?


To use debugging we need breakpoints. Breakpoints are the locations in the line of code from where we want to start debugging. When we setup a breakpoint, at runtime the code stops at the execution of that particular line of code. From that point you can execute each line of code one by one. You can setup multiple breakpoints and visual studio will pause the execution once it reaches those code lines at runtime. Now let's see how to setup a breakpoint.

How To Set Up A Breakpoint?


A breakpoint can be setup by two ways, one is using mouse and another is using keyboard. Both are equally easy. With mouse you just have to click on the vertical light grey bar present on the left, parallel to the code. While clicking, the mouse should be in front of the desired line of code. After click you will notice a red circle appears on the grey bar. See below screenshot for reference.


sample breakpoint in visual studio


Now in case of keyboard, you have to click on the line of code at which you want to create breakpoint and then just press F9 key. Red circle will appear instantly after pressing the key.

Removing a breakpoint is similar to setting it up. Just perform the above steps again and you will notice that the red circle disappears and thus breakpoint is removed.
So above two ways simply toggles the breakpoint, setting/removing a breakpoint.


How To Debug?


Once your breakpoints are setup, you can run your application and the execution of the code will stop at the first breakpoint in it's way. Let's consider an example, I have created a simple Console Application using visual studio and I have put up a breakpoint in sample code as shown below. It has a for-loop and I have added a breakpoint inside it.

how to create breakpoint in visual studio


Create your own console app and replace the code in program.cs with below code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int index = 0;
            for (int i = 0; i < 10; i++)
            {
                index = index + 1; //Incrementing index
            }
        }
    }
}

Now press F5 to run this application, you will notice the execution will be paused at the breakpoint and the active breakpoint statement becomes yellow. At this stage you can check the values of variables by hovering over the variable as shown in the screenshot below


debugging using breakpoint in visual studio


There are four keys that are helpful in debugging:


  • F5 - Runs the execution to the next breakpoint, at starting it hits first breakpoint
  • F9 - Toggles breakpoint, sets/removes
  • F10 - Steps over to next code statement
  • F11 - Steps inside a function code if the current statement has a function call otherwise just steps over to next code statement.

Now if you keep pressing F10 or F11, the code execution line will change to next line and eventually you will again come up to breakpoint statement. Notice the change in value of index variable again. It should have changed right?
Now press F5 and you will notice that the breakpoint now hits again instantly not step by step, as in case of F10/11. Notice the value change again. This is how you debug your code.

You can read/modify variable values and even execute some statements using debugging windows.

There are more topics involved in debugging process, like Debugger windows, conditional breakpoints etc. We will be discussing these things in our next post Advanced Debugging In Visual Studio.

A video tutorial to teach you better:


Happy Learning!!!

Do give us your feedback. We are good listeners. :D

Comments

Popular posts from this blog

Create Android Apps - Getting Started

Kotlin Type Checking and Smart Cast with Examples