简体   繁体   English

如何确定未存储在变量中的return语句的值?

[英]How do you determine the value of a return statement that isn't stored in a variable?

The code I am working with has many recursive method calls that return the results of the method directly without storing it into a variable. 我正在使用的代码有许多递归方法调用,它们直接返回方法的结果而不将其存储到变量中。

public bool foo()
{
  return bar();
}

public bool bar()
{
  return infinitMethodCalls();
}

As a result, debugging is a pain. 因此,调试很痛苦。 How can I determine the value returned by bar() without using a temp variable or following the endless method calls? 如何在不使用临时变量或遵循无限方法调用的情况下确定bar()返回的值?

temp variable example: 临时变量示例:

public bool foo()
{
  return result = bar();
}

The code I am working with is mostly written this way. 我正在使用的代码主要以这种方式编写。

This depends on the version of VS you are using. 这取决于您使用的VS版本。

For Visual Studio 2013 you can show the "Autos" window, it dynamically displays all return values of functions you step over while debugging. 对于Visual Studio 2013,您可以显示“Autos”窗口,它会动态显示您在调试时跳过的所有函数的返回值。 You can find the Autos Window here: Debug => Windows => Autos in your VS Menu. 您可以在此处找到Autos Window:在VS菜单中调试=> Windows => Autos。 (For more info see here ) (有关详细信息,请参阅此处

For Visual Studio 2010 for example you can use IntelliTraces "Calls View" as answered here 对于例如,你可以使用IntelliTraces Visual Studio 2010的“电话查看”作为回答这里

What about using a trace statement. 那么使用trace语句呢? I'm assuming you want to know what the result of each call on the call stack is when debugging the recursion. 我假设你想知道在调试递归时调用堆栈上每次调用的结果是什么。

Note: Having the trace statement at the top of the function will order the output, to the console, from first to last. 注意:在函数顶部使用trace语句会将输出从头到尾排序到控制台。

For example: 例如:

void Factorial(int n) {

    Trace.WriteLine(n);

    if (n == 0)
       return 1;
    else
       return (n * factorial(n-1));

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当JSON变量名称无效且根数组不在键,值对中时,如何在C#中反序列化JSON? - How do you deserialize JSON in C# when a JSON variable name is invalid and the root array isn't in Key, Value pairs? 如果不支持Contains,如何在LINQ to Entities(实体框架)中执行SQL样式的“IN”语句? - How do you do a SQL style 'IN' statement in LINQ to Entities (Entity Framework) if Contains isn't supported? 如何返回第一个不为 null 或为空的字符串变量 - How do I return the first string variable that isn't null or empty 如何使用ReSharper SDK确定方法的返回类型? - How do you determine the return type of a method using the ReSharper SDK? 我们如何返回在 IF 语句中赋值的变量的值? - How do we return the value of a variable whose value is assigned inside an IF statement? 如何在 if 语句中检查可空值 - How do you check for nullable value in an if statement 如何从 if 语句中返回值? - How do I return a value from an if statement? 您如何正确返回值? - How do you return a value properly? 您如何确定标识符是否引用的值为NULL? - How do you determine whether an identifier is referencing something with a NULL value? 如何确定两个HashSets是否相等(按值,而不是通过引用)? - How do you determine if two HashSets are equal (by value, not by reference)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM