How to see the value returned by a method in Visual Studio Debugger?

In Visual Studio’s Debugger, it has always been a pain to not be able to see the value returned by a method if no variable was assigned to it.
Indeed, the debugger would not give a way to see the value returned by the following method:

static string Concat(string p1, string p2)
{
    return p1 + p2;
}

The only way to see the value before it is returned by the method would be to modify (!) the source code and assign a variable to the result before returning it. This is less than ideal as it means we have to stop the debugging session, modify code and restart debugging. It has unfortunately been a trick many developers have had to use throughout the years.
If we take the example here above, we would change the method to something like the following to be able to see its return value at debug time:

static string Concat(string p1, string p2)
{
    string value = p1 + p2;
    return value;
}

 

The good news is that starting from Visual Studio 2013 we do not have to do such trick anymore; we can directly examine the return value of a method even if there is no variable holding the value. We can do that at 2 moments in time:

  1. Just before the method returns: When we step over to the end of the method (the curly brace at the end of the method definition).
  2. Just after the method returns: When we step out of the method (back to the line of code calling the method). Note that the return value will be lost after the next Debugger Step Over.

There are 2 places where we can see the method return value:

  • In the Debugger Immediate window, using the $ReturnValue keyword. To open the Immediate window while debugging, choose Debug -> Windows -> Immediate (or press keyboard shortcut: Ctrl + Alt + I).
  • In the Debugger Autos window. To open the Autos window while debugging, choose Debug -> Windows -> Autos (or press keyboard shortcut: Ctrl + Alt + V, A).

 

Example when Stepping Over to the method end.

The return value will only be visible when we step over to the end of the method (on the curly braces ending the method definition).

In the picture hereunder, $ReturnValue does not return any value as we have not yet reached the end of the method.
Method Return Value Before Method End

In the picture hereunder, we are at the end of the method (on its ending curly brace). At this stage the debugger knows the value returned by the method and $ReturnValue is populated by the debugger. We can enter $ReturnValue in the debugger Immediate window and see that it holds the value returned by the method. The return value is also visible in the debugger Autos window at the entry with the word “returned” in it. The entry is only a placeholder to show the object instance returned by the method (in this case a string) and does not correspond to any actual variable name.
Mothod Return Value At Method End

 

Example when Stepping Out of the method.

In the picture hereunder we have just stepped out of the method which we want to know its returned value. We can see that the return value is made available by the debugger in both the Immediate and the Autos window.
Return Value When Step Out Of Method

Once we step over to the next line of code in the debugger (F10 shortcut), the entry in the Autos window for the return value is lost and in the Immediate window, $ReturnValue does not evaluate to anything anymore.
No Return Value After Step Out Of Method

Reference: http://msdn.microsoft.com/en-us/library/dn323257.aspx

3 Replies to “How to see the value returned by a method in Visual Studio Debugger?”

Leave a Reply

Your email address will not be published.


*