How to use yield to return multiple elements from a function one at a time in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Iterator Function Get_Fibonacci_Sequence(ByVal total As Integer) As IEnumerable(Of Integer)
        Dim returnValue As Integer = 1, prev As Integer = 0

        For i As Integer = 0 To total - 1
            Yield returnValue
            Dim temp As Integer = returnValue
            returnValue = prev + returnValue
            prev = temp
        Next
    End Function

    Public Shared Sub Main()
        For Each i As Integer In Get_Fibonacci_Sequence(11)
            Console.Write("{0} ", i)
        Next
    End Sub
End Class




' run:
'
' 1 1 2 3 5 8 13 21 34 55 89
'

 



answered Aug 3, 2022 by avibootz

Related questions

4 answers 1,498 views
1 answer 198 views
1 answer 211 views
1 answer 164 views
2 answers 267 views
...