C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: The program calls the ToArray extension on that IEnumerable instance. We now have an Integer array.
IntegerTip: ToArray is found within System.Linq. It can be called on any type instance that implements IEnumerable.
Tip 2: Many types implement IEnumerable. And we can even use The IEnumerable type itself—this is what we do in this program.
Enumerable.Range: The example also uses Enumerable.Range to quickly get an IEnumerable of Integers.
Range, Repeat, EmptyVB.NET program that uses ToArray
Module Module1
Sub Main()
' Create an IEnumerable range.
' This is not an array.
Dim e As IEnumerable(Of Integer) = Enumerable.Range(0, 10)
' Use ToArray extension to convert to an array.
Dim array() As Integer = e.ToArray()
' Display elements.
For Each i As Integer In array
Console.WriteLine(i)
Next
End Sub
End Module
Output
0
1
2
3
4
5
6
7
8
9
Info: This Function allocates a new array. It then calls into Array.Copy. This newly-allocated array is returned to the calling Function.
Array.CopyFunctionAlso: Some collections, such as List, offer a separate ToArray Function. This is not an extension method.
List