Is there a definitive naming convention for methods returning IAsyncEnumerable?

There's no better guideline than what the .NET teams already do :

  • ChannelReader.ReadAllAsync returns an IAsyncEnumerable<T>
  • In EF Core 3, results are returned as an IAsyncEnumerable by calling AsAsyncEnumerable()
  • In System.Linq.Async, ToAsyncEnumerable() converts IEnumerables, Tasks and Observables into IAsyncEnumerables
  • All other operators in System.Linq.Async retain their names. There's no SelectAsync or SelectAsAsyncEnumerable, just Select.

In all cases, it's clear what the result of the method is. In all cases, the results of the method need to be awaited with await foreach before they can be used.

So the real guideline remains the same - ensure the name makes the behavior clear:

  • When the name is already clear, eg with AsAsyncEnumerable() or ToAsyncEnumerable(), there's no need to add any suffix.
  • In other cases, add the Async suffix so developers know they need to await foreach the result.

The code analyzers and generators don't really care about the names of methods, they detect smells by inspecting the code itself. A code analyzer will tell you that you forgot to await a Task or await foreach an IAsyncEnumerable no matter how you call the methods and the variables. A generator can simply use reflection to check for IAsyncEnumerable and emit await foreach

It's the style analyzers that check names. Their job is to ensure the code uses a consistent style so developers can understand the code. The style analyzer will tell you that a method doesn't follow the style you chose. That style may be the team's or a commonly accepted style guide.

And of course, everyone knows the common prefix for private instance fields is _ :)


It’s not an async method, so the name shouldn’t end in ‘Async’. That method suffix is a convention to make it obvious that the method should be awaited, or the result handled as a Task.

I think a normal collection-returning name is appropriate. GetFoos(), or similar.

Tags:

C#

C# 8.0