Exercise - Set breakpoints
Breakpoints are used during the debug process pause execution. This enables you to track variables and examine the sequence in which your code is executed. Breakpoints are a great way to start your debug process.
Set a breakpoint
Earlier in this module you completed an exercise where you ran an application in the debugger. The application displayed "greeting messages" in the DEBUG CONSOLE panel. At the end of the exercise, you noticed that the code repeats Andrew's greeting in an unexpected way.
In this exercise, you'll use a breakpoint to help you identify the issue.
Ensure that your Program.cs file contains the following code sample:
/* This code uses a names array and corresponding methods to display greeting messages */ string[] names = new string[] { "Sophia", "Andrew", "AllGreetings" }; string messageText = ""; foreach (string name in names) { if (name == "Sophia") messageText = SophiaMessage(); else if (name == "Andrew") messageText = AndrewMessage(); else if (name == "AllGreetings") messageText = SophiaMessage(); messageText = messageText + "\n\r" + AndrewMessage(); Console.WriteLine(messageText + "\n\r"); } bool pauseCode = true; while (pauseCode == true); static string SophiaMessage() { return "Hello, my name is Sophia."; } static string AndrewMessage() { return "Hi, my name is Andrew. Good to meet you."; }
Use the Visual Studio Code debugger tools to set a breakpoint on the first code line inside the
foreach
loop.Tip
One easy option for toggling on/off a breakpoint is to select (left-click) the area to the left of the line number. Breakpoints can also be set by using the
Run
menu and by using keyboard shortcuts.On the Run menu, select Start Debugging.
Notice that code execution pauses at the breakpoint, and that the current code line is highlighted in the Editor.
On the Debug controls toolbar, select Step Into.
You can hover the mouse pointer over the buttons on the Debug controls toolbar to display the button labels.
Notice that code execution advances to the following code line and pauses:
messageText = SophiaMessage();
This code line assigns the return value of the
SophiaMessage
method to the string variablemessageText
.Take a moment to consider why selecting Step Into produced this result.
- The Step Into button is used to advance to the next executable statement.
- Since the first element in the
names
array isSophia
and theif
statement is checking for the nameSophia
, the expression evaluates totrue
and code execution moves into the code block of theif
statement.
On the Debug controls toolbar, select Step Into.
Notice that code execution advances to the
SophiaMessage
method and pauses.The Step Into button has advanced to the next executable code line. The next executable code line isn't the next line number in the file, it's the next statement in the execution path. In this case, the next executable statement is the entry point to the
SophiaMessage
method.On the Debug controls toolbar, select Step Out.
Notice that code execution returns to the code line that called the
SophiaMessage
method and pauses.Take a moment to consider why selecting Step Out produced this result.
When inside a method, the Step Out button completes the remaining lines of the current method and then returns to the execution context that invoked the method.
On the Debug controls toolbar, select Step Into.
Notice that code execution advances to the following code line and pauses:
messageText = messageText + "\n\r" + AndrewMessage();
Take a moment to consider why execution advanced to this code line.
Although the code indentation implies that this code line is part of the code block for the
else if
statement, it isn't. Using curly braces{}
to define the code blocks for thisif - else if
structure would have helped to avoid this bug. As the code is written, Andrew's message will be added tomessageText
each time the loop iterates.
Verify your code updates
Once you've isolated an issue in your code, you should update your code and then verify that the issue has been fixed.
On the Debug controls toolbar, select Stop.
Take a minute to fix your code logic.
You have a few options for fixing the identified issue in your code. For example:
You could keep the existing code lines and add curly braces
{}
to theif
structure for each code block.You could merge the two code lines that follow the final
else if
statement, forming a single statement as follows:else if (name == "AllGreetings") messageText = SophiaMessage() + "\n\r" + AndrewMessage();
Either way, your updated code must include the call to
AndrewMessage
within the code block whenname == "AllGreetings"
.On the File menu, select Save.
Use the debugger UI tools to clear the breakpoint that you set earlier.
On the Run menu, select Start Debugging.
Verify that your code now produces the expected results.
Hello, my name is Sophia. Hi, my name is Andrew. Good to meet you. Hello, my name is Sophia. Hi, my name is Andrew. Good to meet you.
On the Debug controls toolbar, select Stop.
Congratulations! You've successfully used the Visual Studio Code debugger to help you isolate and correct a logic issue.
Recap
Here are a few important things to remember from this unit:
- Use breakpoints to pause code execution during a debug session.
- Use Step Into from the Debug controls toolbar to observe the next executable code line.
- Use Step Out from the Debug controls toolbar to advance through the current method and back to the code line that called the method.