Using The Debugger

Lesson 4.2: Using the Debugger to Find and Fix Errors


The debugger is one of the most powerful tools in a programmer's arsenal. It allows you to step through your code line by line, inspect variables, and track down the source of errors. In this lesson, you'll learn the basics of using the debugger in Visual Studio or Visual Studio Code.


Why Use a Debugger?


Find Errors Faster: Instead of guessing where the problem is, the debugger lets you see exactly what's happening as your code executes.

Understand Your Code: By stepping through your code, you gain a deeper understanding of how it works.

Save Time: Debugging becomes much more efficient with the right tools.

Key Debugger Features


Breakpoints:

A breakpoint is a marker you place in your code to tell the debugger to pause execution at that point.

Once the program stops at a breakpoint, you can examine the state of variables and step through the code one line at a time.

Stepping:

Step Over: Executes the current line of code and moves to the next line.

Step Into: If the current line is a method call, the debugger steps into the method's code.

Step Out: If you're inside a method, the debugger steps out and resumes execution in the calling code.

Variable Inspection:

The debugger allows you to view the values of variables at any point during execution.

You can see how the values change as your code runs.

Call Stack:

The call stack shows the order in which methods were called, leading to the current line of execution.

This is useful for understanding the flow of your program and identifying where errors occurred.

How to Use the Debugger in Visual Studio


Set Breakpoints: Click in the gray margin next to the line of code where you want the debugger to pause. A red dot will appear.

Start Debugging: Press F5 or click the "Start Debugging" button.

Examine Variables: When the program pauses at a breakpoint, you can hover your mouse over variables to see their values, or use the "Locals" and "Watch" windows to monitor specific variables.

Step Through Code: Use the F10 (Step Over) and F11 (Step Into) keys to move through your code one line at a time.

Stop Debugging: Press Shift+F5 or click the "Stop Debugging" button.

How to Use the Debugger in Visual Studio Code


Set Breakpoints: Click in the margin to the left of the line of code.

Start Debugging: Click the "Run and Debug" icon (a bug with a play arrow) in the sidebar, or press F5.

Examine Variables: Similar to Visual Studio, you can hover over variables or use the "Variables" window.

Step Through Code: Use the icons in the debug toolbar or the corresponding keyboard shortcuts to step over, into, or out of code.

Debugging Tips


Start Small: If your program is large, start by setting breakpoints near where you suspect the error might be.

Isolate the Problem: Try to reproduce the error in a smaller, simpler test case.

Print Statements: Temporarily add Console.WriteLine() statements to print variable values to the console.

Don't Give Up! Debugging can be frustrating, but it's a skill that gets easier with practice.

Example: Debugging a Simple Error


C#

// This code has a logical error – it will always print "Even"

int number = 5;


if (number % 2 == 0) 

{

    Console.WriteLine("Even");

}

else

{

    Console.WriteLine("Odd");

}


By using the debugger, you could step through the code and realize that the if condition is always true because the % operator (modulus) returns 0 when number is even.

Course Syllabus