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 Dictionary to List

Convert a Dictionary to a List. Use the ToList Function and the KeyValuePair type.
Convert Dictionary, List. A Dictionary can be converted to a List. It can be represented as a List of KeyValuePairs. The ToList Function is useful here. With this sample program, we demonstrate this conversion in the VB.NET language.
Example. Let's create a Dictionary instance and set four different keys equal to four different values. Next, the ToList extension method can be used. Please note this method is not available in older versions of the .NET Framework.

Then: We can loop over each KeyValuePair in the List instance with a For-Each loop construct.

KeyValuePairFor Each, For
VB.NET program that converts Dictionary to List Module Module1 Sub Main() ' Example dictionary. Dim dict As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)() dict("cat") = 1 dict("dog") = 2 dict("mouse") = 3 dict("palomino") = 4 ' Call ToList on it. ' ... Use KeyValuePair list. Dim list As List(Of KeyValuePair(Of String, Integer)) = dict.ToList ' We can loop over each KeyValuePair. For Each pair As KeyValuePair(Of String, Integer) In list Console.WriteLine(pair.Key) Console.Write(" ") Console.WriteLine(pair.Value) Next End Sub End Module Output cat 1 dog 2 mouse 3 palomino 4
Discussion. We consider why this conversion is helpful. Why would a programmer want to convert a Dictionary into a List? First, looping over every element in a List is much faster than looping over all the pairs in a Dictionary.

And: Second, Lists will use less memory because no internal buckets array is necessary.

List
On the other hand, Dictionary provides faster lookup and element removal. In most performance-sensitive programs, Dictionary is a better choice. Sometimes, you can use parallel collections and use the faster one as needed.Dictionary
Summary. Here we converted a Dictionary instance into a List instance. A List of KeyValuePairs can always represent the data inside a Dictionary. The main difference between these two representations is in the area of performance.
© 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