【发布时间】:2018-08-08 09:18:37
【问题描述】:
在使用了几年的 HTML/ASP 之后,我又回到了 C# 编程领域。 我遇到了这些行,找不到它的作用。 它是一个类中的一个方法:
private string PeekNext()
{
if (pos < 0)
// pos < 0 indicates that there are no more tokens
return null;
if (pos < tokens.Length)
{
if (tokens[pos].Length == 0)
{
++pos;
return PeekNext();
}
return tokens[pos];
}
string line = reader.ReadLine();
if (line == null)
{
// There is no more data to read
pos = -1;
return null;
}
// Split the line that was read on white space characters
tokens = line.Split(null);
pos = 0;
return PeekNext();
}
它是否会在其他一些 Return 发生之前调用自己?
这里发生了什么,从未见过返回自身的方法!? 什么是返回,空字符串或什么......? 或者我之前只是错过了它。
也许很简单,但让我很困惑。
【问题讨论】:
-
它的递归看起来像
-
它将返回“PeekNext()”方法执行的结果。它是递归的典型,所以我猜在方法体中有一些 if 语句将返回实际值。\
-
如果没有看到其余的方法,我们就无法判断。肯定还有其他返回语句结束递归,否则这个方法总是会导致
StackOverflowException。 -
欢迎来到 SO。请关注stackoverflow.com/help/mcve 提出更好的问题。