Simple C# Code Debugging in Visual Studio
It is almost impossible for any programmer to write clean code all the time without leaving any bugs. Therefore, one of the key skills a good programmer must demonstrate is the ability to debug and find what’s wrong with a piece of code. This indicates a person not only has technical skills, but also analysis ability as well as problem solving skills.
Debugging is an important skill that each developer needs to learn if they want to be productive and efficient in the industry afterwards.
Visual Studio is a new IDE being discovered by many of the students during this semester through our Web Application Development 1 course. It is important for us to be aware that this software provides us with a lot of in-built debugging options to debug our C# applications, compared to when we write a piece of code in HTML, CSS, JS or even PHP using just a text editor.
This article is going to introduce the varying debugging ways as a guide, using which you can do further research online to extend your knowledge of debugging. Here are the points which I am going to mention in the current article:
- Start debugging & Continue execution
- Variable Inspection
- Stepping, Set Next statement, and the Call Stack Window
- Edit and Continue
Start Debugging & Continue Execution
To start debugging a C# application, we need to click the “Start” button to launch the application, or go to the Debug menu and select “Start Debugging”, or simply use the F5 shortcut key.
Once the application starts in debug mode, this changes to “Continue”, allowing us to resume normal execution if ever we arrive at a break state (either at a breakpoint or when stepping through your codes) while debugging an application.
A breakpoint as it names suggest, will simply break the normal execution of a program and tells the debugger to break (pause) the application at the line indicated by the breakpoint. This becomes handy when we need to debug just a specific section of an application, hence allowing the application to run and stop at the specific area desired.
To set a breakpoint at a specific line, simply click in the left margin of the text editor on the line of code we want the debugger to stop on, or place the cursor on that line and press the F9 key. A red circle can then be seen indicating that the breakpoint is set on that line. To remove a breakpoint, simply click the set breakpoint again.
When the debugger stops, one of the first thing we will tend to do is inspect/check the values of the variables at that point in time. This can be achieved in several ways, namely through DataTips, Locals Window, Watch Window, Autos Window and Immediate Window.
Stepping, Set Next Statement, and the Call Stack Window
Once the application has stopped at the desired area to debug, Visual Studio allows us to advance the program execution manually line by line. This helps prevent the developer from having to set breakpoints on every line. Three commands that are used at this stage are the Step Over (F10), the Step Into (F11) and Step Out (Shift + F11).
The Set Next Statement becomes handy to enable us to move the instruction pointer to start executing the program from a new line. This allows us to either skip some lines of code while debugging, or go back and execute some line of code again.
Finally, since most C# applications are event driven, it becomes important to know the execution path that resulted in how our program reached a current location. This is provided by the Call Stack Window, which keeps track of the program execution when running.
Edit and Continue
Once we debug and arrive at the point in our code that most likely needs to be modified, it can be tiresome and time consuming having to each time stop the execution, edit the code and re-run the application again. To help work around this, the Edit and Continue feature will allow us to make any modifications while our code is being debugged and continue execution of the program.
Find Out More
The mentioned techniques above are a brief introduction to basic debugging. To build on that knowledge I recommend some more reading the Getting Started with the Debugger which provides a simple example to illustrate the above-mentioned techniques.