3 Neat Debugging Tips for Visual Studio That You Might Have Never Heard Of

Software category image

The Visual Studio Debugger is a mighty tool with lots of options. Here are 3 non-standard debugging tips for you that can be handy.

Debug your app from the first line of code

Sometimes it’s useful to debug a program right from the beginning. Maybe, there is something happening at the start that you don’t understand. Or maybe, you want to explore step by step what the program does after launching.

In case you don’t know what code part is executed first, you can use this handy tip. Instead of using the debug command or its default hotkey F5, press F10. This will build and run your app, but it stops at the first line of code that is executed.

The Visual Studio debugger interrupts at the first line of code when launched with the F10 hotkey.
The Visual Studio debugger interrupts at the first line of code when launched with the F10 hotkey.

Now, you can step through the app code and see what happens next.

Warning

Apparently, this does not work for .NET MAUI apps!

Stop execution at the current line of code

The Run to cursor feature is useful when you want your app to run up to a specific line of code. You can execute the command via the context menu or with the default hotkey CTRL + F10. This will build and run your app, but the debugger will stop at the designated line. Here is a short demo video:

Let the Visual Studio Debugger stop at the current line of code with the Run to cursor command
Let the Visual Studio Debugger stop at the current line of code with the Run to cursor command

The command creates an invisible breakpoint that does not appear in the Breakpoints window of Visual Studio. Once hit, the breakpoint will be deleted, so the debugger won’t stop if the code is reached a second time. When the debugging session is terminated, the breakpoint is also deleted.

Configure how objects are displayed while debugging

By default, the Visual Studio Debugger will display the representation of the ToString() method of any object when inspected during a debugging session. Here is an easy example:

The Visual Studio Debugger displays the ToString() output of any object when inspected.
The Visual Studio Debugger displays the ToString() output of any object when inspected.

Of course, you could overwrite the ToString() method of the Person object. This can look like this:

The Visual Studio Debugger displays the overriden ToString() output of any object when inspected.
The Visual Studio Debugger displays the overriden ToString() output of any object when inspected.

Sometimes, it is not possible to override the ToString() method, but you still want to be able to customize the output. In such cases, you can use the DebuggerDisplay attribute, which provides greater flexibility and offers many options. For example, you can call methods of the object. Check out the documentation for more details. Here is the output for our example:

The Visual Studio Debugger displays the DebuggerDisplay attribute output of an object when inspected.
The Visual Studio Debugger displays the DebuggerDisplay attribute output of an object when inspected.

Conclusion

By following these tips, you can take your debugging skills in Visual Studio 2022 to the next level.

Find more tips for Visual Studio here: