DSP2017, Tech

Let’s make ‘yield return’ our best friend!

How many programmers you know use ‘yield‘? Seriously, if you have an occasion, ask them – I’m afraid you will notice a strange thing – everybody heard about it but (almost) nobody (including me) uses it. Time to change it! Why? Because ‘yield‘ was introduced in C# 2.0 (yeeeah, it’s that old!) and can help you get rid of some of the ‘temp’ collections in your code and, what’s more important, it may prevent you from System.OutOfMemory exception.

Let’s get rid of temporary collection!

That’s it, it occurs you don’t need at least half of the temporary collections you use! Don’t believe me? Have a look at the example below (full code here).
Let’s assume we have a list of animals species and we want to display only the 3-letters-long ones.
We can do it in a classical approach:

But we have to declare a ‘temp‘ list in a right scope… That’s so boring!

What about the below approach:

 

What does it mean?

(1) yield return data; will in fact pause the iteration and pass a data value to the caller. The caller will operate on the value and when it needs the next value from GetAnimalsWith3LetterNameWithYield() method, the code will be resumed in the line marked with (1) in a comment. So it means line (1) is executed only at the exact moment when it’s needed. Excellent!

(2) yield return “AnimalFromOutsideTheList”; – it just adds additional string to the returning list, just in case you suspected it can be used only in a loop. As you can see – ‘yield‘ can be used in any place but putting it inside a loop has a greater sense ;).

 

What’s next?

In the next posts I will show you how placing yield keyword in your code affects RAM usage. I will also focus on other ‘yield break‘. So stay tuned!

PS

Happy Easter everyone!

Featured image by Patricia Jekki

Share this:

  • Pingback: dotnetomaniak.pl()

  • Adrian Stanisławski

    Actually it is easier to use where. Maybe that is why nobody use yield now. We have other tools in new C#.

    • In that case probably where is better, but I still find it useful in other cases, where there are more things to calculate for example between loops – and you can’t do that easily with LINQ. Which is awesome, but doesn’t solve all problems. 😀

      • Yes yes yes, you both are absolutely right – in ‘real life’ problem similar to the above examples I would also use LINQ ‘where’ ;). My examples were exagerrated but I wanted to make some easy introduction to the ‘yield return’ because in a future post I plann to write some more on the subject.

  • That example doesn’t show true usability of yield, at least not since LINQ was introduced – as noted in previous comment, it’s easily dealt with using where.

    A better example, I think, would be Fibonacci – you could easily hold two previous values and take advantage of what is not available in LINQ. 🙂

    Nice post, and good to know someone still notices this feature.

    • Yeah, Fibonacci numbers is a great example of ‘yield’ power, thanks!
      I think I would use ‘yield return’ to calculate prime numbers, too ;). And there is probably some more complex mathematical algorithms where we could make use of ‘yield’…

      As a side note – ‘yield’ is used sometimes in the interviews so I think it’s worth to know the basics :].

      BTW it’s always nice to know what LINQ guts looks like and that it uses ‘yield return’ too 😉 https://github.com/dotnet/corefx/blob/master/src/System.Linq/src/System/Linq/Take.cs

  • Pingback: Let’s make ‘yield return’ our best friend! - How to Code .NET()