VS get returned value in C# code?

2019-01-19 07:04发布

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?

5条回答
Ridiculous、
2楼-- · 2019-01-19 07:27

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.

查看更多
做自己的国王
3楼-- · 2019-01-19 07:27

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!

查看更多
祖国的老花朵
4楼-- · 2019-01-19 07:35

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.

查看更多
小情绪 Triste *
5楼-- · 2019-01-19 07:42

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

查看更多
走好不送
6楼-- · 2019-01-19 07:42

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.

查看更多
登录 后发表回答