Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS get returned value in C# code?

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?

like image 596
Cristian Diaconescu Avatar asked Dec 10 '22 21:12

Cristian Diaconescu


2 Answers

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.

like image 156
Hans Passant Avatar answered Feb 15 '23 11:02

Hans Passant


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

like image 35
JaredPar Avatar answered Feb 15 '23 10:02

JaredPar