Index · Quick recall Q&A

1 min read
Senior2 min read
Rapid overview

Quick recall Q&A

Q: What's the difference between IEnumerable and IAsyncEnumerable?

IEnumerable<T> uses synchronous iteration with yield return and foreach. IAsyncEnumerable<T> supports async operations with await foreach, allowing each MoveNextAsync() to be awaited. Use IAsyncEnumerable<T> when producing items requires I/O or async work.

Q: How does yield return differ from return?

return exits the method completely. yield return suspends execution, preserving state, and resumes on the next iteration. The method becomes an iterator that produces values lazily.

Q: When would you use Channels over IAsyncEnumerable?

Use Channels when you need decoupled producers and consumers, multiple producers/consumers, backpressure control, or buffering. Use IAsyncEnumerable<T> for simpler pull-based streaming without complex coordination.

Q: What happens if you don't enumerate an iterator?

Nothing executes. Iterator methods use deferred execution - code only runs when you iterate (foreach, ToList, etc.). This enables lazy evaluation and early termination.

Q: How do you handle cancellation in async iterators?

Use [EnumeratorCancellation] attribute on a CancellationToken parameter. The token is automatically threaded through when using WithCancellation() extension method.