Seeing function return values in the Visual Studio 2013 debugger

YanivF

You asked, and we have listened. Seeing return values for functions is something that many .NET developers wanted, and voted heavily for on the Visual Studio uservoice site. This feature exists already for C++ code and the good news is that with the latest version of Visual Studio, it’s here and you’ll be able to use it for your .NET code too.

This new feature allows you to examine the return value of a function when the developer steps over or out of a function during your debugging session. This is especially useful when the returned value is not stored in a local variable. Consider the following nested function example **Foo(Bar()); **in this example you can now examine the return value(s) from Bar and Foo, when you step over that line.

The return value(s) get displayed in the “Autos Windows” (Debug->Windows->Autos) and you can also use the pseudo variable “$ReturnValue” in the Watch and/or Immediate window to fetch the last function’s return value.

 

Here are some steps for you to try it out in action:

1) Create a new C# Console Application

2) Copy the following code to your program.cs

01 class Program
02      {
03          static void Main()
04          {
05              int result = Multiply(Five(), Six());
06          }
07  
08          private static int Multiply(int num1, int num2)
09          {
10              return (num1 * num2);
11          }
12  
13          private static int Five()
14          {
15              return(5);
16          }
17  
18          private static int Six()
19          {
20              return (6);
21          }
22      }

 

3) Put a breakpoint in line # 05 – [int** result =Multiply(Five(), Six());] and run it

4) Run the program (F5)

 

5) When the program breaks step-over the function (F10)

Observe:

6) As this example was nested functions, you are able to see all the return values in the autos window

7)  You can also use the last returned value in your immediate window

 

Note that if a function returns a struct, it will not be displayed in the Autos window with the exceptions of enums and structs that have exactly one field which is the size of a pointer (i.e. structs with the same characteristics as enums).

In closing, we are really happy to bring this capability to you and as always your feedback is welcome in the diagnostics forum.

0 comments

Discussion is closed.

Feedback usabilla icon