There are ways to do that, none of which appear to be good:
http://stackoverflow.com/questions/2055927/ienumerable-and-recursion-using-yield-return[
^]
http://arnonaxelrod.wordpress.com/2010/08/19/traversing-binary-tree-using-an-iterator/[
^]
To summarize:
You either do all the recursion each time you want to yield the next value (very expensive).
Or you run the recursion in another thread and have the enumerator communicate with that thread.
I would suggest a different approach:
Rather that trying to wrap your recursive function in a enumerator, pass a function to your recursive function, something like this:
int MyRecursiveFunction(int k, bool MyItemHandler(int k))
{
if (k == 0) {
MyItemHandler(k);
return k;
} else {
if (MyItemHandler(k))
return k;
else
return MyRecursiveFunction(k - 1, MyItemHandler);
}
}
The idea is to call MyItemHandler on each item that the recursion generates (on the way down).
If MyItemHandler returns true you abort the recursion, and otherwise return false to continue the recursion.
Not sure my example code will quite do what you want, or that the syntax is correct, but it's just to illustrate the idea. The actual implementation I leave to you.