C# Tip: use yield return to return one item at the time (2024)

2021-12-14 4 min read CSharp Tips

Yield is a keyword that allows you to return an item at the time instead of creating a full list and returning it as a whole.

Table of Contents

Just a second!
If you are here, it means that you are a software developer. So, you know that storage, networking, and domain management have a cost .

If you want to support this blog, please ensure that you have disabled the adblocker for this site. I configured Google AdSense to show as few ADS as possible - I don't want to bother you with lots of ads, but I still need to add some to pay for the resources for my site.

Thank you for your understanding.
- Davide

To me, yield return has always been one of the most difficult things to understand.

Now that I’ve understood it (not thoroughly, but enough to explain it), it’s my turn to share my learnings.

So, what does yield return mean? How is it related to collections of items?

Using Lists

Say that you’re returning a collection of items and that you need to iterate over them.

A first approach could be creating a list with all the items, returning it to the caller, and iterating over the collection:

IEnumerable<int> WithList(){ List<int> items = new List<int>(); for (int i = 0; i < 10; i++) { Console.WriteLine($"Added item {i}"); items.Add(i); } return items;}void Main(){ var items = WithList(); foreach (var i in items) { Console.WriteLine($"This is Mambo number {i}"); }}

This snippet creates the whole collection and then prints the values inside that list. On the console, you’ll see this text:

Added item 0Added item 1Added item 2Added item 3Added item 4Added item 5Added item 6Added item 7Added item 8Added item 9This is Mambo number 0This is Mambo number 1This is Mambo number 2This is Mambo number 3This is Mambo number 4This is Mambo number 5This is Mambo number 6This is Mambo number 7This is Mambo number 8This is Mambo number 9

This means that, if you need to operate over a collection with 1 million items, at first you’ll create ALL the items, and then you’ll perform operations on each of them. This approach has two main disadvantages: it’s slow (especially if you only need to work with a subset of those items), and occupies a lot of memory.

With Yield

We can use another approach: use the yield return keywords:

IEnumerable<int> WithYield(){ for (int i = 0; i < 10; i++) { Console.WriteLine($"Returning item {i}"); yield return i; }}void Main(){ var items = WithYield(); foreach (var i in items) { Console.WriteLine($"This is Mambo number {i}"); }}

With this method, the order of messages is different:

Returning item 0This is Mambo number 0Returning item 1This is Mambo number 1Returning item 2This is Mambo number 2Returning item 3This is Mambo number 3Returning item 4This is Mambo number 4Returning item 5This is Mambo number 5Returning item 6This is Mambo number 6Returning item 7This is Mambo number 7Returning item 8This is Mambo number 8Returning item 9This is Mambo number 9

So, instead of creating the whole list, we create one item at a time, and only when needed.

Benefits of Yield

As I said before, there are several benefits with yield: the application is more performant when talking about both the execution time and the memory usage.

It’s like an automatic iterator: every time you get a result, the iterator advances to the next item.

Just a note: yield works only for methods that return IAsyncEnumerable<T>, IEnumerable<T>, IEnumerable, IEnumerator<T>, or IEnumerator.

You cannot use it with a method that returns, for instance, List<T>, because, as the error message says,

The body of X cannot be an iterator block because List<int> is not an iterator interface type

C# Tip: use yield return to return one item at the time (1)

A real use case

If you use NUnit as a test suite, you’ve probably already used this keyword.

In particular, when using the TestCaseSource attribute, you specify the name of the class that outputs the test cases.

public class MyTestClass{ [TestCaseSource(typeof(DivideCases))] public void DivideTest(int n, int d, int q) { Assert.AreEqual(q, n / d); }}class DivideCases : IEnumerable{ public IEnumerator GetEnumerator() { yield return new object[] { 12, 3, 4 }; yield return new object[] { 12, 2, 6 }; yield return new object[] { 12, 4, 3 }; }}

When executing the tests, an iterator returns a test case at a time, without creating a full list of test cases.

The previous snippet is taken directly from NUnit’s documentation for the TestCaseSource attribute, that you can find here.

Wrapping up

Yes, yield is a quite difficult keyword to understand.

To read more, head to the official docs.

Another good resource is “C# – Use yield return to minimize memory usage” by Makolyte. You should definitely check it out!

And, if you want, check out the conversation I had about this keyword on Twitter.

Happy coding!

🐧

C# Tip: use yield return to return one item at the time (2024)
Top Articles
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 6354

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.