You’ve probably heard about debugging. But what it is and how to apply it in C#?
Debugging is an integral part of development during which bugs (errors) are discovered, localized and fixed.
With debugging we can:
- Find out the current value of variables;
- Find out which path the program was running on;
There are many different tools for debugging program, but we will consider the most basic which you cannot do without in any project — Breakpoint.
Breakpoint temporarily stops the application at the line on which breakpoint located. All lines of code up to the breakpoint will be executed, but the line on which the breakpoint is set will not be executed.
Let’s see the breakpoint in action and take the project from the previous lesson.
There are two ways to set breakpoint:
- Click on the desired code line, then press F9;
- Click the mouse opposite the required line
The result will be the same — red dot:
Okay, we can launch a project (F5 or green arrow in the toolbar) and don’t forget to launch in Debug mode, because Visual Studio uses two built-in build configurations — Debug and Release.
In debug mode the program compiles without optimization and have all necessary information for debugging.
Now the program has stopped at a breakpoint and is waiting for our actions. To continue execution press F10 or Step over in toolbar (I prefer use F10).
Program waits until you type a name in the console and press enter,after which the program execution stops on the next line.
When hovering a variable we can see its value and change it dynamically by clicking and typing a new value.
Type F10 one more time and you will see result:
So we looked at the most primitive example with breakpoint. If you need go inside of some method (we will see it in the next section, when will teach a methods), enter F11 or Step Into in toolbar.
If you need to leave from debugging mode enter Shift + F11 or Step Out.
One more interesting thing, you can add Breakpoint Conditions
And here you can configure your breakpoint as you want.
Now you know how to debug and catch bugs 🙂
Previous article -> 1.3 First Program and Install VS 2022
Next article -> 1.5 .NET CLI
Join to our community in Telegram — https://t.me/itifico
If you want to support me, you can buy me a cup of coffee and I will drink it when I write the next article :)
- Dinesh Chintalapudi — 3$
- Unknown — 5$