Member-only story

Using yield with async in C#

An introduction and example about how to use yield within an async method, with a few tips on how to avoid common problems.

Jamie Burns
3 min readApr 7, 2022

Following on from my other recent post about working with yield, I thought I’d share what I learnt when trying to use yield within an async method.

Say we have some logic that calls a ValidateAsync method on a list of inputs, and we return all the error results of this call. It might look something like this:

And, as per the requirements in my previous post, we’re only actually interested in the first 3 errors. We’d call it like this:

Now, if we wanted to convert this to using yield (so that we wouldn’t need to validate all items once we’ve already found our first 3 errors), you might think…

--

--

Jamie Burns
Jamie Burns

Written by Jamie Burns

Software Engineer, founder of Bungalow64 Technologies