Breakpoints
Your program may builds and run, but what if it gives the wrong result? Solution: check the path of your code and the data being stored using breakpoints. Breakpoints are often the first port of call for programmers who have a logical error in their code.
1. Set a breakpoint
To set a breakpoint, decide which line of code you want to check, then click on the grey margin to the left of your code. A dark red dot will appear.
You can also press F9 to toggle a breakpoint on or off.
To remove the breakpoint, simple click again.
2. Run the program.
When you run your code (F5) the program will automatically stop at the breakpoint.
3. Watch the variable
Method 1: Hover over the variable to reveal its current value. Use the little pin if you wish to keep the value displayed as you step through the code.
Method 2: Add a watch by:
- Right-click
- Select the variable
- Right-click
- Select Add Watch.
The variable will appear at the bottom of your screen.
4. Step to the next line of code.
Press F10 to step through each line of code. This will also allow you to examine the flow control of your program i.e. the sequence in which your code runs. You can also press F5 to just run the rest of the program without any more checking.
Want to know more about programming in C#?
Want to know more theory for GCSE?