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
16 Super Fun Things to Do in Nashville | Carful of Kids Travel
The week in politics: Tennessee Gov. Bill Lee signs new gun bill. What will it do?
Durr Burger Inflatable
Explore Tarot: Your Ultimate Tarot Cheat Sheet for Beginners
라이키 유출
Uvalde Topic
Taylor Swift Seating Chart Nashville
Saberhealth Time Track
Mbta Commuter Rail Lowell Line Schedule
Gdlauncher Downloading Game Files Loop
Star Wars: Héros de la Galaxie - le guide des meilleurs personnages en 2024 - Le Blog Allo Paradise
Niche Crime Rate
Khiara Keating: Manchester City and England goalkeeper convinced WSL silverware is on the horizon
Nick Pulos Height, Age, Net Worth, Girlfriend, Stunt Actor
How to Create Your Very Own Crossword Puzzle
Site : Storagealamogordo.com Easy Call
Td Small Business Banking Login
Azpeople View Paycheck/W2
Jenna Ortega’s Height, Age, Net Worth & Biography
The Many Faces of the Craigslist Killer
Jcp Meevo Com
Ficoforum
Narragansett Bay Cruising - A Complete Guide: Explore Newport, Providence & More
HP PARTSURFER - spare part search portal
Tim Steele Taylorsville Nc
Ff14 Sage Stat Priority
Dtlr On 87Th Cottage Grove
Xfinity Outage Map Lacey Wa
Shiftwizard Login Johnston
Babbychula
Save on Games, Flamingo, Toys Games & Novelties
Family Fare Ad Allendale Mi
Reborn Rich Ep 12 Eng Sub
Magicseaweed Capitola
The disadvantages of patient portals
Timberwolves Point Guard History
Thelemagick Library - The New Comment to Liber AL vel Legis
Registrar Lls
2023 Fantasy Football Draft Guide: Rankings, cheat sheets and analysis
Barstool Sports Gif
All Obituaries | Sneath Strilchuk Funeral Services | Funeral Home Roblin Dauphin Ste Rose McCreary MB
Gym Assistant Manager Salary
Smite Builds Season 9
Az Unblocked Games: Complete with ease | airSlate SignNow
Pgecom
Best Conjuration Spell In Skyrim
Centimeters to Feet conversion: cm to ft calculator
Dobratz Hantge Funeral Chapel Obituaries
Here’s What Goes on at a Gentlemen’s Club – Crafternoon Cabaret Club
Where and How to Watch Sound of Freedom | Angel Studios
Lux Nails & Spa
Bellin Employee Portal
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 6443

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.