Suppose I wanted to enumerate through a given directory's files & directories iteratively, in such a way that the inner directories and files are given out first.
If you were to execute this enumerable in a foreach loop with the functions: DeleteFile and DeleteEmptyDirectory it should not fail, because the most inner items are yielded out first.
Now of course one could do this (pseudocode):
func(Directory dir)
foreach (var f in dir.EnumerateFileSystemInfos())
if (f is FileInfo)
yield return f;
else
foreach (var item in func(f))
yield return item;
yield return dir;
But that has wastes a lot of allocations on enumerators.
One could also use two stacks and "acquire" all of the directories together and then just keep popping out items but that wastes too much memory and doesn't balance the MoveNext times that well.
Any ideas on how to do this efficiently without wasting too much space?