Can I find out the return value before returning while debugging in Visual Studio?

Viewed 97304

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?

21 Answers

Not that I know of. Note that if you do add a variable, it will get removed by the compiler in release builds anyway...

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

The value can only be seen directly after returning from the function, thus the easiest way to access it is by putting a breakpoint on the function call and step over (F10) the call.


Update for VS2015: boo! unfortunately, it doesn't appear to be in VS2015 (devenv v14)
Update for VS2017: it's back. (devenv v15)

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.

There are a lot of workarounds, but none seems satisfactory.

To quote John Skeet below (comment on a now-deleted answer):

Still looks inconvenient to me - especially if you don't know which return value you're going to need before you start debugging. I really don't want to have to have a temporary variable cluttering up my code every time I ever return anything.t

In theory, the debugger could have a return-variable. After all: it's just a variable on the stack:

unsafe {
  int * sp = stackalloc int[1];
  try {
    return a+b;
  }
  finally {
    Trace.WriteLine("return is " + *(sp+3));
  }
}

So consider this a feature request for Visual Studio.

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).

You can also ask to evaluate the value in the intermediate window as well, if it does not set flags or other variables, but only returns something.

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.

In VS2019, Just go to Debug->Windows->Autos window. There, you see concat return value as shown below:

enter image description here

You could try to select "someTableAdapter.getSomeData();", right click on it, and go for Quick Watch.

Drag and drop the return expression into a watch window.

For example, in the statement

return someTableAdapter.getSomeData();

drag and drop

someTableAdapter.getSomeData()

into a watch window, and you'll see the value.

You can do this for any expression.

Related