VB.NET IEnumerable ExamplesUse the IEnumerable Interface with arrays, Lists, and For-Each loops.
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. We can use Extensions, such as ToList, on an IEnumerable type.

Range, Repeat, Empty


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.


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
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.


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

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.


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.

© 2007-2021 sam allen. send bug reports to info@dotnetperls.com.