GusBedasi / YieldReturn2

A code sample of using two different ways to deal with lists, the most conventional way and the yield approach, all measured with benchmark

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using Yield return

This is another example of using Yield return instead of convetionally return lists.

Benefits

When you use yield return you can work each piece of data as you need it, theres no need to create the whole list on the memory, improving the application resource management and the time, GC, etc has a greater improving as you can see on the image below:

Benchmark result:

img

Observation

When you use yield return you necessarily need to return a IEnumeral<something> so the compiler can generate for you the Enumerable and Enumerator implementation to deal with each return of element, follows a gist of the code genereated by the compiler.

compiler generated code example

About

A code sample of using two different ways to deal with lists, the most conventional way and the yield approach, all measured with benchmark


Languages

Language:C# 100.0%