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.

Note

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:

  1. Right-click in the text editing window to add a breakpoint to your code.

  2. Start debug mode with the run button as you might normally run your program.

  3. Make note of the debugger panes and how you might make use of them:

    1. 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.
    2. Locals pane shows the value of local variables and parameters within the program being debugged.
    3. Add variables, parameters, and expressions to the Watch pane to monitor their values while debugging.
    4. 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.
  4. Debugger Code Stepping Buttons:

    1. Step over button moves debugger to the next line to be executed within a method.
    2. Step out button brings the debugger out of the execution of a method.
    3. 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.
  5. Right-click on the breakpoint to set conditional logic for when you want the breakpoint to run.

  6. Stop debug mode wth the stop button.

Visual Studio for Mac Extra Tips

  1. Your IDE may not default to Debug mode. To select for it, in the top menu, select Debug > Start Debugging.
  2. 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.
  3. 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.
  4. For more information on using the Debugger in Visual Studio for Mac, check out this guide .

Check Your Understanding

Question

True or False: Breakpoints on Console.WriteLine() are helpful because stepping into them reveals what is printed in the console.

Question

Define a breakpoint.

  1. A point in our code where the debugger will stop running and provide information about the current state.
  2. A point in our code that we anticipate will result in an exception or error.
  3. A point in our code where we include a print statement to see what’s going on.
  4. A point in our code where we want to throw the computer out of a window because nothing works.