Debugging in .NET apps using Visual Studio Part 1

hootanht

Hootan Hemmati

Posted on January 13, 2023

Debugging in .NET apps using Visual Studio Part 1

I’m getting back from cycling right now and I think about the relationship between car and bicycle in debugging, sounds weird but very interesting to me.
I am cycling, and I can see all the detail on my way until I arrived at my destination but when I am driving a car, I can’t see the true detail of my ways like asphalt surface or plants growing in between rocks and asphalt. In debugging we set behind the bicycle and cycle step by step in the code and when we are in release mode, we don’t want to see much detail in our way just two things are important to us, first is it must be working and second is we must arrive to our destination. So, let’s get to know about some great tools in our cycling that makes our way smoother.

1. Debugging mode

On the top of the visual studio, we have 2 options for choosing our journey mode. The first option is Debug mode and by default, it is selected. In debug mode, we have many options to track our code and see all the details step by step.

Image description

2. Release mode

The second option is releasing mode, this mode is designed to test our app in the final version and virtualize our customers' environments.

Image description

3. Breakpoints

Breakpoints are the signs of what we want to check after running the program.
They can be easy use by clicking next to the line number in visual studio.

Image description

After the breakpoint is set, when we run our program in debug mode, we can see the program stop in the line we want to see the special behavior or track the code.

Image description

When the debugger arrived at our breakpoint the line color is changed to yellow and which means that the line doesn’t execute right now but with the F10 key you can execute the code. We talk about the executing code after hitting breakpoints later so don’t worry about this.

Change the behavior of executed line

We can easily change our line executer (yellow arrow on the left side of code number) behavior to run what we want in the visual studio.
The first way is you can simply drag and drop the executed line at which code line you want to just execute that line.

Image description

Then you can press F10 to execute the ‘’c” variable and then you can see the debugger ignore the “a” and “b” variables and their default value are 0.

Image description

So that’s the easy way but let’s think about whether we have a 1M line, does it works in the big solutions? The answer is no but don’t worry because the visual studio developer team developed this great feature. So, let’s learn the second way for doing this thing.
Are you ready? Just simply right clicks and chose the next statement option for going to that exact line and executing that.

Image description

Alright but if we want to run the previous line until that execution run, what can we do? Do we have any solutions? Happy to answer that question and that is yes!
Like the previous situation we have two ways to do that, first is just to hove a mouse in the specific line we want to execute with the previous code lines and select the green run (play) button.

Image description

The second way is simply to right-click on the execute line you want and then just chose the run to cursor option.
Image description

In the next part of this series of articles we read about how to step into our code (like F10 and F11) and then we go deeply into the breakpoints feature like condition breakpoints.
I hope you like this collection of my articles and enjoy that.

💖 💪 🙅 🚩
hootanht
Hootan Hemmati

Posted on January 13, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related