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 Convert List and Array

Convert Lists and arrays with the ToArray and ToList Functions.
Convert List, array. Lists and arrays are often converted. Both the List and array types in the VB.NET language are useful. But in some program contexts we require the opposite type. We do this with the extension methods ToArray and ToList.Array
Example. Initially, this example creates a new List of Integer types and then adds three elements to it. Then, it invokes the ToArray extension method on that collection and assigns the result to a new reference local variable of array type.Integer

Next: It invokes the ToList Extension on the array and assigns the result to another List variable.

Program: This shows how any List and array of equivalent element type can be converted back and forth.

ToList
VB.NET program that uses ToArray and ToList Module Module1 Sub Main() ' Create a list and add three elements to it. Dim list As List(Of Integer) = New List(Of Integer) list.Add(1) list.Add(2) list.Add(3) ' Convert the list to an array. Dim array As Integer() = list.ToArray Console.WriteLine(array.Length) ' Convert the array to a list. Dim list2 As List(Of Integer) = array.ToList Console.WriteLine(list2.Count) End Sub End Module Output 3 3
Newer Frameworks required. The ToArray and ToList extension methods are part of the System.Linq namespace in the .NET Framework. This namespace was introduced in .NET 3.5. If you target an older Framework, this example will not work.

Tip: In this case, you can use the List constructor or imperative copying of elements in loops.

List
Summary. We invoked the ToArray and ToList extension methods to declaratively and clearly convert between list and array types in the VB.NET language. We described the limitations of these methods. They must receive compatible element types.
© 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