Using Count Property Of Dictionary Class In VB.NET

In this article I explained about Count Property of Dictionary Class.
  • 4836

Dictionary Properties

The Dictionary class has three properties – Count, Keys and Values.

Count

The Count property gets the number of key/value pairs in a Dictionary. The following code snippet display number of items in a dictionary.

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.WriteLine()

        Console.WriteLine("Number of key/value pair in Dictionary:")

        Console.WriteLine("Count: {0}", StudentDetailList.Count)

        Console.ReadLine()

    End Sub

End Module

Output:

 CountProperty-of-dictionaryClass.jpg

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.