C# 在 Visual Studio 中调试时,我可以在返回之前找出返回值吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/268048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:34:56  来源:igfitidea点击:

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

c#visual-studiovisual-studio-debugging

提问by doekman

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

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

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?

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

采纳答案by Marc Gravell

Not that I know of. Note that if you doadd 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 $ReturnValuein the watch/immediate window.

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

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.

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



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

VS2015 更新:嘘!不幸的是,它似乎没有出现在 VS2015 (devenv v14)
VS2017 更新中:它回来了。(devenv v15)

回答by Yann Semet

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

您可以尝试选择"someTableAdapter.getSomeData();",右键单击它,然后转到Quick Watch

回答by Biri

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.

您也可以要求评估中间窗口中的值,如果它没有设置标志或其他变量,但只返回一些东西。

回答by LeopardSkinPillBoxHat

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.

使用 Shift-F11 退出 go() 方法,然后在“Autos”调试窗口中,它将显示刚刚从堆栈中弹出的方法调用的返回值(在本例中,go() 方法是你想要什么)。这是 Visual Studio 2005 中的行为;我没有使用过 Visual Studio 2008,所以我不知道在那个版本中它的行为是否相同。

回答by GeekyMonkey

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.

打开调试→自动窗口让您关闭。它不会显示实际的返回值,但会显示在 return 语句中计算的内容。

回答by Sylvain Rodrigue

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

我知道的唯一方法是在返回行上放置一个断点,然后调用Quick Watch窗口并输入返回的表达式:

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

但这仅在调用不更改任何对象的状态时才有效(因为当您恢复执行时将再次调用相同的方法)。

回答by Pita.O

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.

您可以对任何表达式执行此操作。

回答by doekman

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

有很多解决方法,但似乎没有一个令人满意。

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

引用下面的 John Skeet(评论现已删除的答案):

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:

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

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.

因此,请将此视为 Visual Studio 的功能请求。

回答by Sprintstar

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

Microsoft Visual C++ 曾经这样做过,但 Visual Studio 没有 AFAIK .. :(

回答by Alex Angas

This can be done in Visual Studio 2013 with CLR 4.5.1 according to the customer feedback site. It was not available in previous versions for C#.

根据客户反馈站点,这可以在带有 CLR 4.5.1 的 Visual Studio 2013 中完成。它在 C# 的早期版本中不可用。

(Visual Studio 2008 and earlier supported it for VB.NET. It has always been available to C/C++ developers.)

(Visual Studio 2008 及更早版本支持 VB.NET。它一直可供 C/C++ 开发人员使用。)