Why Learn Debugging Early?
Everyone writes bugs. The difference between a beginner and a professional is how quickly they find them. The debugger lets you pause the program and look at every variable, so you stop guessing.
A Program with a Bug
The program crashes with IndexOutOfRangeException. Let us find out why using the debugger.
Step 1: Set a Breakpoint
Click in the gray margin left of the line total += scores[i]; (or press F9). A red dot appears. The program will pause there.
Step 2: Start Debugging
Press F5 (Start Debugging). The program runs until it reaches the red dot and stops.
Step 3: Look and Step
| Key | Name | What it does |
|---|---|---|
| F5 | Continue | Run until the next breakpoint |
| F10 | Step Over | Run the current line and move to the next |
| F11 | Step Into | Go inside a method that this line calls |
| Shift + F11 | Step Out | Finish the current method and return |
| Shift + F5 | Stop | End the debugging session |
Hover over i and scores.Length to see their values. After several F5 presses you will see that i becomes 3, but the array only has indexes 0, 1 and 2.
<=. It should be <. An array with 3 items has indexes 0, 1, 2.
Useful Debugger Windows
| Window | Use it to |
|---|---|
| Locals / Variables | See all variables in the current method |
| Watch | Type any expression, such as total / scores.Length, to track it |
| Call Stack | See which methods called the current one |
| Breakpoint conditions | Right-click a breakpoint and pause only when i == 2 |
Printing Is Fine Too
Fast Feedback with dotnet watch
The program re-runs automatically every time you save. Great for learning.
Common Compiler Errors
| Error | Meaning | Fix |
|---|---|---|
| CS1002: ; expected | Missing semicolon | Add ; at the end of the line |
| CS0103: name does not exist | Typo or variable out of scope | Check spelling and where it was declared |
| CS0246: type or namespace not found | Missing using or package | Add the using line or dotnet add package |
| CS0029: cannot convert type | Wrong type assigned | Convert, for example int.Parse(text) |
| CS8602: possible null reference | Nullable warning | Check for null before using the value |
| CS0165: use of unassigned variable | Variable not given a value | Initialize it, for example int x = 0; |
Conclusion
Set a breakpoint, step through and inspect variables instead of guessing. Learn to read the first compiler error carefully.

