Bite-sized Visual Studio Debugging Tips

Joel Lyons
Building Degreed
Published in
5 min readNov 30, 2017

Visual Studio is a powerful development environment, with so many features it’s easy to miss some things that might make your debugging more efficient. Here are some useful debugging tips to help you unleash the power!

Debugging Javascript

When debugging a web application, you can put breakpoints in your Javascript code, making it super easy to see what’s going on. Just click on the line of interest and hit F9.

You can specify which web browser should be launched by clicking the little chevron next to the Start button in the toolbar, then clicking Set as Default in the Browse With dialog.

Although setting the default browser is done within the Browse With dialog, you don’t actually want to click the Browse button from that dialog. That will simply launch the application in the browser without debugging it. After clicking Set as Default, cancel out of that dialog and then click Start or hit F5 to start debugging!

Take Control of the Watch Window

To make your life easier when watching .NET variables (watch window, data tip, etc.) add a DebuggerDisplay attribute to your classes. The debugger will display this interpolated string instead of the .NET type information.

Or, you can override the ToString method. Using the ToString approach is nice because the expressions are checked by the compiler. Expressions in the DebuggerDisplay attribute are not. In both cases, keep the code very simple because watched variables are evaluated each time you step (F10) in the debugger.

More Than Just Locals

The “Locals” debug window shows the current value of all variables/parameters in the local scope, but also includes some extra info, such as the value returned by the last method you called out to and the exception that was just thrown (if there is one). The screenshot here is when debugging JavaScript but it works with .NET as well.

Right Click!

Right click in the various debug windows (Watch, Output, Call Stack, etc.) while debugging to see all the ways they can be customized.

Visual Studio can really help you track down bugs and test your code more efficiently!

Put It On Ice

While debugging, the “Threads” debug window shows the threads of execution in the processes you are attached to. In this window, you can right-click and “freeze” a thread so it will not get processor time until you thaw it. This is useful if you’re stepping through code in one thread and another thread is also executing and it keeps stealing focus. Just freeze that other thread so the debugger can ignore it. Then thaw it when you’re done so it can finish doing its job.

More Useful Breakpoints

You can set conditional breakpoints, which will only stop execution when an expression is true (such as {list.Length > 10}), or when the value of a specified variable changes, or when that line of code has been hit a specified number of times. Just right click on a breakpoint and unleash the power!

Tracepoints

Sometimes, while debugging, it’s handy to know when something occurs and perhaps what a variable value is at the time. Or perhaps you want to see how many times or how often a method is getting called. Instead of adding a breakpoint or adding a call to Debug.WriteLine or Log.Verbose, add a tracepoint. Add a breakpoint to the line of code, right click on it, and pick “Action”. Specify what you’d like to see in the output window when that line executes. You can even include variable values or simple expressions in curly braces.

And remember, if your debug output window is too cluttered, right click in there and hide some things.

Perf Tips

After you’ve hit a breakpoint and are stepping over the code, notice the time taken by each line is shown in a small “perf tip”.

Now, that sounds like something to keep an eye on while you’re stepping through your new code before you commit it! Unfortunately, this perf tip is not available when debugging JavaScript.

Pin Your Variables

When stepping through code while debugging in Visual Studio, you can hover over variables and parameters and a “data tip” will pop-up showing current values. You can “pin” the data tip so it hangs around, like a mini version of the Watch window. You can debug into other frames of the stack or even come back in a later debug session and the “data tip” will still be there, showing the current/latest value.

You can even add a comment or export the data tips for someone else to use in their debugging. Nice!

Let ‘Er Run

When stepping through code, you might decide it’s getting tedious and you want the code to just continue ’til it hits a line later in the same method or even higher or lower in the callstack. Instead of navigating to that later location and adding a breakpoint, you can right click on the code (or in the Call Stack window) and use Run to Cursor. Or, even easier, you can just hover over the line and click the little green “run to here” arrow.

Get Help With Exceptions

If an exception occurs when you’re debugging, you might (depending on settings) see the Exception Helper dialog. You can “View Details” or jump to the Exception Settings Window, where you can configure whether the debugger should break when each type of exception occurs or when it goes unhandled (the default).

Also, when an exception occurs in the debugger, a $exception variable is automatically added to the Locals window to give you easy access to the exception object’s details.

Visual Studio is full of awesome little features that can make your life easier, and Microsoft is adding more all the time. Don’t waste debugging time by not knowing what Visual Studio is capable of. Unleash the power!

--

--