Lesson 30 of 30
Debugging and Unit Testing
C# in Visual Studio 2026 — a hands-on guide for developers at every level.
Debugging in Visual Studio 2026
Visual Studio's debugger is one of the most powerful tools in your arsenal. Master it to find bugs in minutes instead of hours.
Breakpoints
Click in the left margin of any line (or press F9) to set a breakpoint. Press F5 to run — execution stops at the breakpoint and you can inspect every variable.
- Conditional breakpoints: Right-click a breakpoint → Conditions. Only stops when a condition is true (e.g.
i == 42). - Tracepoints: Log a message without stopping execution.
Debug Windows
| Window | Shows |
|---|---|
| Locals | All variables in the current scope |
| Watch | Variables and expressions you add manually |
| Immediate | Evaluate C# expressions on the fly |
| Call Stack | Chain of method calls that led here |
| Autos | Variables recently used |
Unit Testing with xUnit
// Install: dotnet add package xunit
// dotnet add package xunit.runner.visualstudio
public class MathHelperTests
{
[Fact]
public void Add_TwoPositiveNumbers_ReturnsSum()
{
int result = MathHelper.Add(3, 4);
Assert.Equal(7, result);
}
[Theory]
[InlineData(0, 0, 0)]
[InlineData(-1, 1, 0)]
[InlineData(100, 200, 300)]
public void Add_VariousInputs_ReturnsCorrectSum(int a, int b, int expected)
=> Assert.Equal(expected, MathHelper.Add(a, b));
}
Running Tests
Open Test Explorer (Test → Test Explorer) and click Run All Tests. Green = pass, red = fail. Click any failing test to see the expected vs actual values.
🎉 Congratulations!
You have completed all 30 lessons of C# in Visual Studio 2026. You now have a solid foundation in C# — from variables and loops, through OOP, async programming, and testing. Keep building!