Today’s Tips’n’Trick is one which I value a lot. Lets say you are in the debugging session and you look at the Call Stack window to check what methods were invoked so far. You can see assembly names, method names, line numbers – even parameter types and names. But you cannot see parameter values! If you want to check what was the value of the input parameter you have to double click the line and check it’s value in Autos or Locals window. To compare values between methods you will have to jump back and forth.
But wait! There is a better way to do it. Right click in the Call Stack window and select Show Parameter Values option. Once selected, the parameter values will be displayed in the Call Stack window along with parameter names and types. This feature is extremely useful when debugging recursive functions.
If the parameter is a complex type, you can control what will be displayed by annotating the type with DebuggerDisplayAttribute.
In the last Visual Studio Debugger Tips’n’Tricks I showed Run To Cursor option which allows for running debugger and breaking in selected line. Today I am going to present another option called Set Next Statement.
Set Next Statement (shortcut: Ctrl + Shift + F10) is very useful feature and allows to move execution pointer to selected statement. This can be used to not execute statement(s), like skipping line 27 in above example. It can also be used to run statement(s) again, by simply selecting Set Next Statement on the line already executed. When combined with editing variable values, this is a very powerful tool.
If you a mouse person, you can easily Set Next Statement by moving yellow arrow () on the left.
Imagine, you are in the debugging mode and the next statement is a loop. You do not want to step into this loop and waste time on iterating over items but you also do not want to step out of the method. What you can do is to place cursor in the line you would like the debugger to break and select Run To Cursor option from context menu (shortcut: Ctrl + F10). It is an easy way to jump through the code, breaking in chosen places to peek on variable values.
You can even use Run To Cursor for starting an application and breaking in selected line.
When debugging your code and you want to step into one of the “outer” methods, either FormatResult or PrintMessage in the above example, then right click in the code window and select Step Into Specific option. The Visual Studio will show you list of all methods you can step into. Just click the one you want to debug and debugger will break there.
How many times did you step into a method just to realise you didn’t want to? Sometimes it is a simple, short method and you can step through it. Other times it is one of those monstrosities spanning for dozens of lines, or a recursive function. Instead of stopping debugger and starting again to avoid the mistake you can actually step out from the method. Just click icon on the toolbar, or use Shift + F11 shortcut and the debugger will take you out.