Debugger
Some of the features that you will find yourself leaning on the most as you further your career in tech is the Visual Studio debugging tools.
The links for each debugging feature below are for Windows users. Mac users will find that Visual Studio for Mac has the same exact features, but those features may look slighlty different or be in a slightly different location. We have provided some extra tips for Mac users at the end in case you haven’t been able to locate these tools in your copy of Visual Studio for Mac.
Open up your examples repo
and find the HelloMethods
project. Read through each item, make note of how the debugging tools work for each item, and practice using the tool on HelloMethods
:
Right-click in the text editing window to add a breakpoint to your code.
Start debug mode with the run button as you might normally run your program.
Make note of the debugger panes and how you might make use of them:
- Autos pane shows the values of parameters and variables on the line that the debugger is currently sitting on, as well as the line above.
- Locals pane shows the value of local variables and parameters within the program being debugged.
- Add variables, parameters, and expressions to the Watch pane to monitor their values while debugging.
- Call Stack pane displays the record of the methods that have been called in the program being debugged.
- View a list of breakpoints in the Breakpoints pane. You may also disable and enable breakpoints from here.
Debugger Code Stepping Buttons:
- Step over button moves debugger to the next line to be executed within a method.
- Step out button brings the debugger out of the execution of a method.
- Step into button makes the debugger enter the method at which it is currently paused. Note that
you can’t step into
System
defined methods, only those defined by your program.
Right-click on the breakpoint to set conditional logic for when you want the breakpoint to run.
Stop debug mode wth the stop button.
Visual Studio for Mac Extra Tips
- Your IDE may not default to Debug mode. To select for it, in the top menu, select Debug > Start Debugging.
- To view the debugging panes, select Debug in the top menu and scroll down to Windows. Select the items you wish to view, ie. Breakpoints, Watch, etc.
- To add conditions to a breakpoint, right click on the breakpoint and select Edit Breakpoint. From menu that opens, use the Advanced Conditions section to set your conditions for when you want the breakpoint to be executed.
- For more information on using the Debugger in Visual Studio for Mac, check out this guide .
Check Your Understanding
True or False: Breakpoints on Console.WriteLine()
are helpful because stepping into them reveals what is printed in the console.
Define a breakpoint.
- A point in our code where the debugger will stop running and provide information about the current state.
- A point in our code that we anticipate will result in an exception or error.
- A point in our code where we include a print statement to see what’s going on.
- A point in our code where we want to throw the computer out of a window because nothing works.