Getting a Method's Return Value in the VS Debu

2019-01-23 13:31发布

Is it possible to get a method's return value in the Visual Studio debugger, even if that value isn't assigned to a local variable? For example, I'm debugging the following code:

public string Foo(int valueIn)
{
    if (valueIn > 100)
        return Proxy.Bar(valueIn);
    else
        return "Not enough";
}

Since I'm not setting any local variables in Foo, and assuming I'm not setting a break point in whatever's calling Foo, is there a way to see what the return value is if I have a breakpoint inside of Foo (or another way)? I don't have much experience with the Autos or Intermediate windows, so I'm not sure if those are even a valid option or not.

8条回答
放荡不羁爱自由
2楼-- · 2019-01-23 13:51

You can set a breakpoint in Foo, open the immediate window and run the following command:

? Foo(valueIn)

This will print the return value in the Immediate Window.

You can also copy the expression and paste it into the Watch window, though I would do this only if I am certain that the call has no side effects (otherwise you can get confusing results).

查看更多
我命由我不由天
3楼-- · 2019-01-23 13:53

Visual Studio 2013 now has the ability in the Autos window to display the last value returned by a function, alleviating the need to re-execute it in the Output window or introduce a temporary variable:

http://blogs.msdn.com/b/visualstudioalm/archive/2013/06/27/seeing-function-return-values-in-the-debugger-in-visual-studio-2013.aspx

查看更多
戒情不戒烟
4楼-- · 2019-01-23 13:56

You can always switch to disassembler view and step through the individual instructions. The return value will be in @eax (or @rax) just before you execute the 'ret' instruction.

查看更多
孤傲高冷的网名
5楼-- · 2019-01-23 14:00

No, I don't know of a way to do this. I would put a breakpoint in the caller and look at the return value there.

查看更多
我命由我不由天
6楼-- · 2019-01-23 14:06

You can also highlight any expression in the debugger and right-click -> quick watch. That will execute the expression (assuming it's valid) and give you the value.

查看更多
迷人小祖宗
7楼-- · 2019-01-23 14:06

You can always use your watch box to evaluate function calls.

Foo(valueIn);

This will only work if your Proxy.Bar(valueIn) is time independent though.

查看更多
登录 后发表回答