Can I find out the return value before returning w

2019-01-01 12:51发布

Take the following function:

DataTable go() {
    return someTableAdapter.getSomeData();
}

When I set a breakpoint in this function, is there a possibility to inspect the returned value? go() is directly coupled to a datagrid in an .aspx page.

The only way to inspect the returned datatable is to use a temporary variable. However, that's a bit inconvenient. Isn't there another way?

20条回答
旧时光的记忆
2楼-- · 2019-01-01 13:20

Opening the Debug → Autos window gets you close. It won't show the actual return value, but it will show what was evaluated in the return statement.

查看更多
裙下三千臣
3楼-- · 2019-01-01 13:21

According to the currently accepted answer by Marc Gravell:

This functionality has been added to Visual Studio 2013. You can see the return values in the autos windows or use $ReturnValue in the watch/immediate window

That answer also stated that this functionality does not work in Visual Studio 2015. This is not (entirely) true. On Examine return values of method calls there is the following note:

You must have the legacy expression evaluators turned on for $ReturnValue to be recognized (Tools / Options / Debugging / Use the legacy C# and VB expression evaluators). Otherwise, you can use $ReturnValue1.

I tested this in Visual Studio 2015 Enterprise:

  • With legacy expression evaluators turned off: only $ReturnValue1 works
  • With legacy expression evaluators turned on: both $ReturnValue and $ReturnValue1 work
查看更多
残风、尘缘若梦
4楼-- · 2019-01-01 13:21

Step out of the go() method using Shift-F11, and then in the "Autos" debug window it will show the return value of the method call which just popped off the stack (in this case, the go() method which is what you want). This is the behaviour in Visual Studio 2005; I haven't used Visual Studio 2008 so I don't know if this behaves the same way in that version.

查看更多
旧人旧事旧时光
5楼-- · 2019-01-01 13:23

The only way I know is to place a breakpoint on the return line and then call the Quick Watch window and enter the returned expression:

someTableAdapter.getSomeData();

But this only works if the call does not change the state of any object (since there will be a second call to the same method when you will resume the execution).

查看更多
闭嘴吧你
6楼-- · 2019-01-01 13:24

Microsoft Visual C++ used to do this, but Visual Studio doesn't AFAIK.. :(

查看更多
后来的你喜欢了谁
7楼-- · 2019-01-01 13:26

I agree that this is a very useful thing to have: not only seeing the return value of the method before stepping out of it, but also seeing the return value of methods I just stepped over. I implemented it as part of a commercial extension to Visual Studio called "OzCode".

With it, you can view method return values right on the code editor, as sort of a HUD-display:

Statement Visualization

For more information, please see this video.

查看更多
登录 后发表回答