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 ToArray Extension Example

Use the ToArray extension Function to convert an IEnumerable to an array.
ToArray. The IEnumerable type can store collections of elements. But in VB.NET programs, we often want arrays—not IEnumerable instances.IEnumerable
ToArray usage. We use the ToArray extension to convert any IEnumerable type to an array. Common IEnumerable types include Lists and query expressions from LINQ.LINQArray
An example. This program creates a new IEnumerable instance with 10 elements in it. The IEnumerable is not an array, but is just a sequence of elements that can be looped over.

Then: The program calls the ToArray extension on that IEnumerable instance. We now have an Integer array.

Integer

Tip: 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, Empty
VB.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
Internals. I investigated the ToArray extension Function. It is located within System.Core.dll in the .NET Framework's folder. I opened this file with IL Disassembler.IL Disassembler

Info: This Function allocates a new array. It then calls into Array.Copy. This newly-allocated array is returned to the calling Function.

Array.CopyFunction
Performance. By looking at its implementation, we can determine there is no performance benefit to calling ToArray. It can improve program syntax.

Also: Some collections, such as List, offer a separate ToArray Function. This is not an extension method.

List
A summary. We explored the ToArray extension Function from System.Linq. We used it with an IEnumerable instance and the Enumerable.Range Function. And we investigated its internals.
© 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