Using Keys Property Of Dictionary Class In VB.NET
In this article I explained about Key Property of Dictionary Class.
Keys
The Keys property gets a collection containing the keys in the Dictionary. It returns an object of KeyCollection type. The following code snippet reads all keys 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()
' Get and display keys
Dim keys As Dictionary(Of String, Int16).KeyCollection = StudentDetailList.Keys
For Each key As String In keys
Console.WriteLine("Key: {0}", key)
Next
Console.WriteLine()
Console.ReadLine()
End Sub
End Module
Output:
