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
Legion Farming Guide Path of Exile - POE Maxroll.gg
Guide:Gwennen gambling | PoE Wiki
Napa Autocare Locator
THE 10 BEST Women's Retreats in Germany for September 2024
Southeast Iowa Buy Sell Trade
Www.craigslist Augusta Ga
Bluegabe Girlfriend
Embassy Suites Wisconsin Dells
Hello Alice Business Credit Card Limit Hard Pull
Luciipurrrr_
Hope Swinimer Net Worth
Bahsid Mclean Uncensored Photo
Blackwolf Run Pro Shop
Craigslist Red Wing Mn
Tamilyogi Proxy
Att.com/Myatt.
Iroquois Amphitheater Louisville Ky Seating Chart
Evil Dead Rise Showtimes Near Regal Sawgrass & Imax
European city that's best to visit from the UK by train has amazing beer
Living Shard Calamity
How to Make Ghee - How We Flourish
Ontdek Pearson support voor digitaal testen en scoren
Troy Gamefarm Prices
Chime Ssi Payment 2023
2487872771
Craigslist Hunting Land For Lease In Ga
Meta Carevr
Villano Antillano Desnuda
Sandals Travel Agent Login
Skepticalpickle Leak
897 W Valley Blvd
Till The End Of The Moon Ep 13 Eng Sub
Craigslist Middletown Ohio
Wcostream Attack On Titan
Unlock The Secrets Of "Skip The Game" Greensboro North Carolina
Blue Beetle Movie Tickets and Showtimes Near Me | Regal
1-800-308-1977
Unity Webgl Player Drift Hunters
42 Manufacturing jobs in Grayling
Conroe Isd Sign In
Encompass.myisolved
Tryst Houston Tx
Studentvue Calexico
Pixel Gun 3D Unblocked Games
Caesars Rewards Loyalty Program Review [Previously Total Rewards]
Dragon Ball Super Card Game Announces Next Set: Realm Of The Gods
A jovem que batizou lei após ser sequestrada por 'amigo virtual'
Online College Scholarships | Strayer University
Argus Leader Obits Today
Slug Menace Rs3
Blog Pch
Guidance | GreenStar™ 3 2630 Display
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 5367

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.