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条回答
Lonely孤独者°
2楼-- · 2019-01-23 14:11

A workaround is to use a Pascal-style result variable:

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

This is good style in my opinion for longer functions. For very short ones like the one above it could be considered overkill, but it does get around the debugger problem.

查看更多
相关推荐>>
3楼-- · 2019-01-23 14:16

The answer of similar question out there: In VS 2013, you can add the variable $ReturnValue to the watch. It contains the actual return value from a function.

Credit to Jesper Jensen.

查看更多
登录 后发表回答