TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Iterator Example: Yield Keyword

Use an Iterator Function. Specify the Yield keyword to return intermediate values.
Iterator. An Iterator returns values when repeatedly called. This kind of function maintains its state between calls. We use the Yield keyword to return a value from an Iterator. Control then resumes next after the Yield.Keywords
Example. This is an example Iterator. The ComputePower Function generates a series of numbers with an increasing exponent value. We pass, as arguments, two values—the base number and the highest exponent required.

While: In ComputePower we iterate through the exponents. We compute the running total and store it in the local variables.

Yield: With the Yield keyword we "yield" control of ComputePower to Main. When next called, we resume after Yield.

VB.NET program that uses Iterator, Yield Module Module1 Sub Main() ' Loop over first 10 exponents. For Each value As Integer In ComputePower(2, 10) Console.WriteLine(value) Next End Sub Public Iterator Function ComputePower( ByVal number As Integer, ByVal exponent As Integer) As IEnumerable(Of Integer) Dim exponentNum As Integer = 0 Dim numberResult As Integer = 1 ' Yield all numbers with exponents up to the argument value. While exponentNum < exponent numberResult *= number exponentNum += 1 Yield numberResult End While End Function End Module Output 2 4 8 16 32 64 128 256 512 1024
In this language, the Yield and Iterator keywords trigger code generation. Special classes are generated. In these classes, the state of Iterators are maintained. This is all invisible to programmers.

However: It is important to know these are not low-level features. They use keywords and short syntax, but much code is needed.

Performance. For performance, a simple For-loop or List is a better choice. Generate values, place into an array, and return. This avoids much of the excess classes that an Iterator will generate behind the scenes.For Each, ForListArray
Summary. The Iterator and Yield features in VB.NET are advanced. They appear simple, but complex code is generated behind the scenes. We often call Iterators within a For Each loop. Values can be returned based on any logic.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf