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);
}


Categories: .Net

Tags: , , , ,

1 reply

  1. It’s worthwhile saying that the “Add” method prohibits a key duplicate by throwing an exception. By using the indexed assignment, an item is actually replaced whenever the key matches an existent one.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: