The topic of recursion with an IEnumerable
was covered in this 2010 SO Post. I'm curious how the story has changed with the advent of IAsyncEnumerable<T>
in .Net Core 3.X? Specifically:
- Should I still be concerned about the resource cost of creating iterators recursively, for deep trees, as Jon Skeet warned?
- In that same SO post, someone provided a "generic non-recursive extension method" to achieve depth first iteration without recursion. If that same approach should be used today, what does an updated version of that
RecursiveSelect()
extension look like forIAsyncEnumerable<T>
?
My interest is performing a depth first search of a (remote) file system, where each iteration causes one of the remote files to be downloaded locally. To that end, I intend to create an asynchronous "generator" (an iterator whose length is unknown) which I can iterate with await foreach
.
As a test case, I'm first simply trying to do a depth-first-search of a local directory...copying the files I find to some other arbitrary folder.
I want to be sure I'm using IAsyncEnumerable
effectively.
Note: Even if creating vast numbers of IAsyncEnumerable
iterators is not expensive for a deep tree, it still makes sense to use the non-recursive approach because the non-recursive approach is probably capable of "check-pointing" its progress and can "resume" if something caused the application to crash. I'm not sure the same could be done for the recursive approach.