2009-10-06 36 views
0

我写了一个递归函数。出于某种原因,它在第一次运行后不会自行调用。它只是从循环中取得下一个项目而不会更深入。我正在使用Visual Studio 10,并且缺乏睡眠。未能递归

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); 
    } 
} 

回答

8
this.GeneratePath(presentDepth); 

应该

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