取以下函数:

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

当我在这个函数中设置断点时,是否有可能检查返回值?Go()直接耦合到.aspx页面中的数据网格。

检查返回数据表的唯一方法是使用临时变量。然而,这有点不方便。没有别的办法了吗?


当前回答

据我所知没有。请注意,如果你添加了一个变量,它将被编译器在发布版本中删除…

更新: 这个功能已经被添加到VS2013中。 您可以在自动窗口中看到返回值,或者在监视/立即窗口中使用$ReturnValue。

该值只能在函数返回后直接看到,因此访问它的最简单方法是在函数调用上设置断点,然后跳过(F10)调用。


VS2015更新:boo!不幸的是,它似乎不在VS2015 (devenv v14) VS2017更新:它回来了。(devenv v15)

其他回答

据我所知没有。请注意,如果你添加了一个变量,它将被编译器在发布版本中删除…

更新: 这个功能已经被添加到VS2013中。 您可以在自动窗口中看到返回值,或者在监视/立即窗口中使用$ReturnValue。

该值只能在函数返回后直接看到,因此访问它的最简单方法是在函数调用上设置断点,然后跳过(F10)调用。


VS2015更新:boo!不幸的是,它似乎不在VS2015 (devenv v14) VS2017更新:它回来了。(devenv v15)

是的,有一个很好的方法。一个显著的缺点是你必须等待5年,也许6年。自从我看到你在2008年11月发的帖子,我建议你waaaaaa…

……aaaait。瞧!只是为了你,微软发布了最新的Visual Studio 2013,它是在调试模式下运行时从菜单中访问的默认功能(菜单debug→Windows→Autos)。

根据微软的说法,没有办法用托管代码可靠地实现这一点。这是他们意识到并正在努力解决的问题:

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

接受的答案在Visual Studio 2015中不能正常工作,但是通过在方法的最后一行上放置断点并按F10,它将把返回值的所有表达式放入locals窗口。

有很多变通办法,但似乎没有一个令人满意。

下面引用约翰·斯基特的话(对现已删除的答案的评论):

对我来说还是不方便 特别是如果你不知道是哪一个 返回你需要的值 在开始调试之前。我真的 不想要有一个临时的 变量混乱我的代码每 每次我还东西的时候

理论上,调试器可以有一个返回变量。毕竟:它只是堆栈上的一个变量:

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

因此,请将此视为Visual Studio的一个特性请求。