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 IEnumerable Examples

Use the IEnumerable Interface with arrays, Lists, and For-Each loops.
IEnumerable. An IEnumerable collection contains elements. We use the For-Each loop in VB.NET to loop over these elements. Lists and arrays implement this interface. We can use Extensions, such as ToList, on an IEnumerable type.Range, Repeat, EmptyLINQ
Example. First, this program creates a List(Of String) and a String array. It then passes these to a custom Display Sub—it receives an IEnumerable(Of String) argument. The array and List are implicitly cast to their IEnumerable interface.Array

Then: In the Display Sub, we use the For-Each loop to enumerate the elements within the IEnumerable collection argument.

For Each, For

So: We can act upon a List and an array with the same code, by using the IEnumerable interface implemented by both types.

VB.NET program that uses IEnumerable Module Module1 Sub Main() ' Create a List. Dim list1 As List(Of String) = New List(Of String) list1.Add("one") list1.Add("two") list1.Add("three") ' Create a String array. Dim array1(2) As String array1(0) = "ONE" array1(1) = "TWO" array1(2) = "THREE" ' Pass both to the Display sub. Display(list1) Display(array1) End Sub Sub Display(ByVal argument As IEnumerable(Of String)) ' Loop over IEnumerable. For Each value As String In argument Console.WriteLine("Value: {0}", value) Next Console.WriteLine() End Sub End Module Output Value: one Value: two Value: three Value: ONE Value: TWO Value: THREE
Example 2. Next, the IEnumerable interface type has other uses. Many Extension methods that act upon IEnumerable are implemented. These are available in all VB.NET programs based on recent .NET Frameworks.

Here: We employ the ToList extension. This converts any IEnumerable type into a List. The element type remains the same.

ToList

Cast: In this example, we implicitly cast the array into an IEnumerable reference. This step is not required.

Note: We use this statement to show how the array is an IEnumerable instance, as well as an array.

VB.NET program that uses IEnumerable, Extension Module Module1 Sub Main() ' An array. ' ... This implements IEnumerable. Dim array1() As String = {"cat", "dog", "mouse"} ' Cast the array to IEnumerable reference. Dim reference1 As IEnumerable(Of String) = array1 ' Use an extension method on IEnumerable. Dim list1 As List(Of String) = reference1.ToList() ' Display result. Console.WriteLine(list1.Count) End Sub End Module Output 3
Discussion. The IEnumerable interface is used throughout VB.NET programs. It is a critical abstraction in the .NET Framework. Many Functions are built upon this abstraction. Even the For-Each loop, a core construct, directly uses IEnumerable.Interface

Tip: In most programs, we will not need to implement IEnumerable ourselves. This is possible, but is not covered here.

Instead: A thorough understanding of how to use IEnumerable on built-in types, including Lists and arrays, often has more benefit.

Summary. We explored the important IEnumerable Interface type in the VB.NET language. This interface is needed for the For-Each loop. It can be used to act upon arrays, Lists and many other collections with a unified code path.
© 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