Using The New IAsyncEnumerable Interface In .NET Core 3

The release of .NET Core 3 last month includes a brand new IAsyncEnumerable interface in the System.Collections.Generic namespace. The Microsoft documentation simply states,
Exposes an enumerator that provides asynchronous iteration over values of a specified type.
This new interface sounds interesting, so I looked at the code in my open-source assembly to see if I could implement it somewhere. I chose the code for LoadFiles() in the DirectoryHelper class that loads files from a collection of directories. The code that looks like this,
  1. public static IEnumerable<FileInfo> LoadFiles(IEnumerable<DirectoryInfo> directories, string searchPattern, SearchOption searchOption)  
  2. {  
  3.     var files = new List<FileInfo>();  
  5.     foreach (var directory in directories.Where(directory => directory.Exists).Select(directory => directory))  
  6.     {  
  7.         files.AddRange(directory.EnumerateFiles(searchPattern, searchOption));  
  8.     }  
  10.     return files.AsEnumerable();  
  11. }  
Using LoadFiles() is simple.
  1. foreach (var file in DirectoryHelper.LoadFiles(searchFolders, "*.*", SearchOption.AllDirectories))  
  2.  {  
  3.      // Process File  
  4.  }  
With some changes, I turned LoadFiles() into a new method called LoadFilesAsync(). The code now looks like this,
  1. public static async IAsyncEnumerable<IEnumerable<FileInfo>> LoadFilesAsync(IEnumerable<DirectoryInfo> directories, string searchPattern, SearchOption searchOption)  
  2. {  
  3.     var options = new EnumerationOptions() { IgnoreInaccessible = true };  
  5.     if (searchOption == SearchOption.AllDirectories)  
  6.     {  
  7.         options.RecurseSubdirectories = true;  
  8.     }  
  10.     var dirs = directories.Where(directory => directory.Exists).Select(directory => directory);  
  12.     foreach (var directory in dirs)  
  13.     {  
  14.         var files = await Task.Run(() => directory.EnumerateFiles(searchPattern, options));  
  15.         yield return files;  
  16.     }  
  17. }  
As you can see above, I changed the return type from,
async IAsyncEnumerable<IEnumerable<FileInfo>>
Then I wrapped the call to directory.EnumerateFiles() in a Task.Run(). After getting the files in each directory, I yield back the collection of files to the calling method. So, using LoadFilesAsync() is slightly different using the new await foreach() in .NET Core 3.
  1. await foreach (var files in DirectoryHelper.LoadFilesAsync(searchFolders, "*.*", SearchOption.AllDirectories))  
  2. {  
  3.     foreach (var file in files)  
  4.     {  
  5.         // Process file  
  6.     }  
  7. }  
Each time the files are returned from the call to EnumerateFiles(), when it hits the yield, it returns that list back to the calling code for processing. That way the calling code show above will be able to iterate over the list of files, while LoadFilesAsync() loads the next list of files. Now lest test the performance.


I wanted to see if one of these methods were more performant than the other.
Using the New IAsyncEnumerable Interface in .NET Core 3
As you can see, for the number of files tested with my benchmark test, LoadFilesAsync() is 5.44ms faster and allocates 101,143 byes less of memory. Make sure to benchmark your own code the implements IAsyncEnumerable.


I hope that you will check out this new interface in .NET Core 3 for use in your projects. If you have any comments or questions, please make them below.

McCarter Consulting
Specializing in software architecture, Microsoft .NET, mentoring, resume review/prep.