c# - 递归失败

标签 c# visual-studio algorithm recursion

我写了一个递归函数。由于某种原因,它在第一次运行后不会调用自己。它只是从循环中取出下一个项目,而不深入。我正在使用 Visual Studio 10,而且 sleep 不足。

public IEnumerable<string> GeneratePath(int depth)
{
    int presentDepth = depth;
    char currentPosition = this.workingPath[presentDepth];
    presentDepth++;

    foreach (char nextPosition in this.moveTable.GetPossibleNextPositions(currentPosition))
    {
        this.workingPath[presentDepth] = nextPosition;

        if (presentDepth < (ChessPiece.targetDepth - 1))
        {
            Console.WriteLine("At least this wasn't ignored:{0}", new string(this.workingPath));
            this.GeneratePath(presentDepth);
        }
        else
            yield return new string(this.workingPath);
    }
}

最佳答案

this.GeneratePath(presentDepth);

应该是

foreach (var i in this.GeneratePath(presentDepth))
{
  yield return ...
}

关于c# - 递归失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1524619/

相关文章:

c# - 动态 Linq 在 GroupBy 之后选择组中的第一个

c# - ASP.NET - 是否可以将用户代理放入对象中?

c# - 使用open xml和C#将图像插入到word文档中

c++ - 无法在 Visual Studio 2015 中将 typedef 转换为 std::pair

visual-studio - 在 VS 2012 Premium 中删除未使用的 Using

c# - Entity Framework 。外键引用不存在的项目

algorithm - 什么广告组合最赚钱

algorithm - 对知道它们以前的项目进行排序

java - 我如何计算重复的单词?

windows - 从 Visual Studio 2013 命令提示符追加到 INCLUDE 环境变量