How can work with Dictionaries through VB.Net & C#?

ms_visual_studio

What is Dictionaries ?

Dictionaries are collections that are meant to store lists of key/value pairs to allow lookup of values based on a key.

For example, assume that you needed to map student code to the full name of some students. For this purpose you can use the Hashtable.

[VB.Net]

 Private Sub CreateHashTable()
        Dim StudentCodes As New Hashtable()
        'The first method. 
        StudentCodes.Add("MN145098", "Mehran,Jn")
        'The second method.
        StudentCodes("MN18454") = "Arman,Nas"
    End Sub

[C#]

private void CreateHashTable()
{
    Hashtable StudentCodes = new Hashtable();
    //The first method. 
    StudentCodes.Add("MN145098", "Mehran,Jn");
    //The second method.
    StudentCodes["MN18454"] = "Arman,Nas";
}

In this way you can read HashTable

[VB.Net]

  For Each Item As DictionaryEntry In StudentCodes
            Console.WriteLine(Item.Value)
        Next

[C#]

foreach (DictionaryEntry Item in StudentCodes) {
    Console.WriteLine(Item.Value);
}
Advertisement