Reading Dictionary Items Using VB.NET

This article explains how to read items from dictionary in VB.NET.
  • 6180

Reading Dictionary Items

The Dictionary is a collection. We can use the foreach loop to go through all the items and read them using they Key ad Value properties.

For Each student As KeyValuePair(Of String, Int16) In StudentDetailList

            Console.WriteLine("Key: {0}, Value: {1}", student.Key, student.Value)

Next

The following code snippet creates a new dictionary and reads all of its items and displays on the console.

Imports System.Collections.Generic

Module Module1

    Sub Main()

        ' Create a dictionary with string key and Int16 value pair

        Dim StudentDetailList As New Dictionary(Of String, Int16)()

        StudentDetailList.Add("Mahesh", 35)

        StudentDetailList.Add("Raj", 25)

        StudentDetailList.Add("Suresh", 29)

        StudentDetailList.Add("Nitin", 21)

        StudentDetailList.Add("Aman", 84)

        ' Read all data

        Console.WriteLine("Student Details List:")

        Console.WriteLine()

        For Each student As KeyValuePair(Of String, Int16) In StudentDetailList

            Console.WriteLine("Key: {0}, Value: {1}", student.Key, student.Value)

        Next

        Console.ReadLine()

    End Sub

End Module

Output:

 ReadingItems-from-dictionary.jpg

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.