yield break 문은 현재 열거자를 벗어나는 경우를 제외하고는 잘 작동하는 다음 메서드가 있습니다. 이것이 왜 그런지 이해하지만 재귀 적 스택을 통해 yield break를 propogate하는 방법에 대해 비어 있습니다.yield break를 사용하여 반복적 인 IEnumerable <T> 루프에서 벗어나는 방법은 무엇입니까?
Top 2 aa
Top 2 ab
Top 2 ac
: 나는 다음 노드를 가지고 통과 할 때
private static IEnumerable<Node> FindChildrenById(IEnumerable nodes, string parentText) {
var en = nodes.GetEnumerator();
var targetFound = false;
while (en.MoveNext()) {
var node = en.Current as Node;
if (node != null)
{
if (node.Parent == null && string.IsNullOrEmpty(parentText))
{
//Returns the top level nodes if an empty parentIdis entered
targetFound = true;
yield return node;
}
else if (node.Parent != null && node.Parent.Text == parentText)
{
//returns the nodes belonging to the parent
yield return node;
}
else
{
//Recurse into the children to see whether one of these is the node to find
foreach (var nd in FindChildrenById(node.Nodes, parentText))
{
yield return nd;
}
}
}
}
if (targetFound)
{
yield break;
}
}
그래서 "최고 2"는 parentText ...
Top 1
Top 1 a
Top 1 b
Top 2
Top 2 a
Top 2 aa
Top 2 ab
Top 2 ac
Top 2 b
Top 3
Top 3 a
Top 3 b
Top 4
로 ... 그때는 결과를 얻을 수 이것은 올바른 결과입니다. 그러나 코드를 단계별로 실행하면 가장 바깥 쪽 루프가 Top 3 및 Top 4를 계속 처리합니다.이 외부 루프에서 어떻게 벗어나나요?
감사합니다 @ 룬 FS. 나는 이것을 시도하고 다시보고 할 것이다. –