4

When debugging C/C++ (unmanaged?) code in VS, after stepping out of a function, you can see the returned value in the 'autos' window:

alt text http://img156.imageshack.us/img156/6082/cpp.jpg

However, this does not work for C# code:

alt text http://img120.imageshack.us/img120/9350/38617355.jpg

Any suggestion on how to get the return value other than cluttering the code with temporary variables?

Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233

5 Answers5

7

It is actually visible. Debug + Other Windows + Registers. Look at the value of EAX (RAX in x64). The value of simple integral types are returned in the EAX register. Long in EDX:EAX. Floating point in STx (XMM00 in x64).


This has been hard to implement, the jitter determines how methods return a value and different jitters will make different choices. Particularly so when the return value type isn't simple, like a struct. If it is large then the jitter will reserve stack space on the calling method and pass a pointer to that space so the called method can copy the return value there. Nevertheless, VS2013 finally made it available, currently available in preview. Visible in the Autos window and by using the $ReturnValue intrinsic variable in the Immediate window and watch expressions.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
6

Unfortunately cluttering your code with temporary variables in the only way in managed code (C# or VB). The CLR has no support for "managed return values" in the debugger and hence VS does not either.

In C++ this feature is a bit simpler. C++ can just grab the register or stack location of the last return value. It doesn't have to deal with issues like a JITer and garbage collection. Both of which greatly complicate a feature such as this.

If you'd like this feature I strongly encourage you to file a feature request or vote for an existing one on connect

https://connect.microsoft.com/VisualStudio

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • at least in 2010, VB seems to be able to handle it fine, so it would seem that the CLR has whatever support is needed. Am I missing something? http://blog.sublogic.com/2010/12/11/showing-c-method-return-in-debugger-vb-net-can-do-it/ – James Manning Dec 14 '10 at 04:25
  • 1
    @James short answer: no CLR did not add this feature, it's an artifact of how VB.Net generates code. Longer Answer: VB.Net supports the notion of returning from a function by assigning the function name the return value (VB6 feature i believe). This is done by creating a hidden local with the same name of the function and matching return types. The compiler in debug will translate return statements into assignments to this local and then returns of the local (the IL is very instructive here). The debugger understands this local and displays it – JaredPar Dec 14 '10 at 17:11
1

Visual Studio 2013 added this capability to C# and VB code. Please check it out in the Preview http://www.microsoft.com/visualstudio/eng/2013-preview and let us know your feedback.

Rui Sun
  • 11
  • 1
0

It's finally implemented in VS 2013. Read the long story on the VS blog.

In short: stepping out of or over a method call populates the Autos window with the result(s) of the called method(s). Images speak better than words:

  1. Start debugging.
    enter image description here

  2. Step over method call(s).

  3. Profit! Notice that the return values of all the nested method calls are displayed. Nice!

    enter image description here

Download VS 2013 Preview to try for yourself.

Bonus! 64-bit code Edit-and-Continue is also implemented!

Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
0

You can set up your Main to return an int, if having a return value from Main() helps you, but yuou will not see the returned value of the test() routine, as Jared has mentioned. So, you do have to clutter up code if you want to see values.

Gregory A Beamer
  • 16,870
  • 3
  • 25
  • 32