yield
This is an old revision of the document!
yield permits to return a part of a result in a executing loop.
Please look at the following code that does not uses yield:
foreach cFile as string in GetFiles( "c:\" )
WriteLine( cFile )
next
return
function GetFiles( cFolder as string ) as List<string>
local aFiles as string[]
local oFiles as List<string>
WriteLine( "start creating list" )
oFiles := List<string>{}
aFiles := Directory.GetFiles( cFolder )
foreach cFile as string in aFiles
oFiles:Add( cFile )
next
WriteLine( "end creating list" )
return oFiles
As you would expect, this code shows the follwing output:
start creating list end creating list c:\bootmgr c:\BOOTNXT c:\bootTel.dat c:\hiberfil.sys c:\lang.txt c:\pagefile.sys c:\swapfile.sys
If you change the code to use yield, you need to change the type of your function - and your function cannot more have any return statement:
foreach cFile as string in GetFiles( "c:\" )
WriteLine( cFile )
next
function GetFiles( cFolder as string ) as IEnumerable<string>
local aFiles as string[]
local oFiles as List<string>
WriteLine( "start creating list" )
oFiles := List<string>{}
aFiles := Directory.GetFiles( cFolder )
foreach cFile as string in aFiles
yield return cFile
next
WriteLine( "end creating list" )
This code produces the following output:
start creating list c:\bootmgr c:\BOOTNXT c:\bootTel.dat c:\hiberfil.sys c:\lang.txt c:\pagefile.sys c:\swapfile.sys end creating list
yield.1524391822.txt.gz · Last modified: 2018/04/22 10:10 by wolfgangriedmann