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:27

Yeah, by switching to VB.NET. ;P (You did just say "Visual Studio". ;)

For as long as I can remember (from Visual Basic through all versions of VB.NET), you can simply query the function name. It "functions" like a local variable that's implicitly declared at the start of the function and its current value is also used as the return value whenever the function exits via non-return statement means (i.e. Exit Function or just falling through) and of course, when the return statement is used.

It is also set to the return statement's expression. Just like a local variable, its value can be inspected at any point of execution inside the function (including after the return statement is executed). C# doesn't have this and should.

That little VB.NET feature (plus the Exit Function statement which it enables - another feature C# doesn't have and should) is very useful in a form of defensive programming I practice where I always initialize the function name to the failure/default value as the first statement. Then, at any failure point (which normally occurs much more often than success points), I can simply call the Exit Function statement (i.e. without having to duplicate the failure / default expression or even a constant/variable name).

查看更多
闭嘴吧你
3楼-- · 2019-01-01 13:30

According to Microsoft, there is no way to implement this reliably with managed code. This is a problem they are aware of and are working on:

For those out there who have experience debugging native C++ or VB6 code, you may have used a feature where function return values are provided for you in the Autos window. Unfortunately, this functionality does not exist for managed code. While you can work around this issue by assigning the return values to a local variable, this is not as convenient because it requires modifying your code. In managed code, it’s a lot trickier to determine what the return value of a function you’ve stepped over. We realized that we couldn’t do the right thing consistently here and so we removed the feature rather than give you incorrect results in the debugger. However, we want to bring this back for you and our CLR and Debugger teams are looking at a number potential solutions to this problem. Unfortunately this is will not be part of Visual Studio 11.

https://connect.microsoft.com/VisualStudio/feedback/details/597933/add-a-return-pseudo-variable-to-the-visual-studio-debugger-for-net-code

查看更多
低头抚发
4楼-- · 2019-01-01 13:31

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)

查看更多
孤独寂梦人
5楼-- · 2019-01-01 13:31

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.

查看更多
无与为乐者.
6楼-- · 2019-01-01 13:31

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.

查看更多
几人难应
7楼-- · 2019-01-01 13:32

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

查看更多
登录 后发表回答