Home
VB.NET
IEnumerable Examples
This page was last reviewed on Feb 9, 2024.
Dot Net Perls
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.
Interface benefits. We can use Extensions, such as ToList, on an IEnumerable type. Query expressions, part of LINQ, can be done with IEnumerables as well.
LINQ
IList
Interface
Example. We can use the IEnumerable type to reference types like lists and arrays. This can help programs become smaller and easier to maintain.
Step 1 We create a List and a String array. These types both implement IEnumerable.
Array
Step 2 We pass the list and array to a custom Display Sub—it receives an IEnumerable(Of String) argument.
Step 3 In the Display Sub, we use the For-Each loop to enumerate the elements within the IEnumerable collection argument.
For
Important We can act upon a List and an array with the same code, by using the IEnumerable interface implemented by both types.
Module Module1 Sub Main() ' Step 1: create a List and String array. Dim list1 As List(Of String) = New List(Of String) list1.Add("one") list1.Add("two") list1.Add("three") Dim array1(2) As String array1(0) = "ONE" array1(1) = "TWO" array1(2) = "THREE" ' Step 2: pass both to the Display sub. Display(list1) Display(array1) End Sub Sub Display(ByVal argument As IEnumerable(Of String)) ' Step 3: loop over IEnumerable. For Each value As String In argument Console.WriteLine("Value: {0}", value) Next Console.WriteLine() End Sub End Module
Value: one Value: two Value: three Value: ONE Value: TWO Value: THREE
Example 2. IEnumerable 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
Detail 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.
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
3
Discussion. The IEnumerable interface is a critical abstraction in .NET. Many Functions are built upon this abstraction. Even the For-Each loop, a core construct, directly uses IEnumerable.
Tip In most programs, we will not need to implement IEnumerable ourselves. This is possible, but is not covered here.
Detail A thorough understanding of how to use IEnumerable on built-in types, including Lists and arrays, often has more benefit.
Summary. We explored the IEnumerable type in VB.NET. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 9, 2024 (edit link).
Home
Changes
© 2007-2024 Sam Allen.