Dictionary, How to read-write data in using C# and VB.net

What is Dictionaries ?

Dictionary is a collection object which is used to store data in key and value pairs. In this, object Key values must be always unique and it won’t accept any null or duplicate values because we use keys to identify values stored in dictionary objects but values can be duplicated or we can set null values also.

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

The Dictionary collection is a part of generic collections so to use Dictionary object in our applications we need to add the following namespace in our applications.

C# Dictionary class is a generic collection of keys and values pair of data. The Dictionary class is defined in the System.Collections.A generic namespace is a generic class and can store any data type in a form of keys and values. Each key must be unique in the collection.

[VB.Net]

Private Sub mycodetips()
Dim mycodetips As New Hashtable()
‘The first method.
mycodetips.Add(“MN145098”, “Mehran,Jn”)
‘The second method.
mycodetips(“MN18454”) = “Arman,Nas”
End Sub

[C#]

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

The way you can read the data

[VB.Net]

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

[C#]

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


Discover more from mycodetips

Subscribe to get the latest posts sent to your email.

Discover more from mycodetips

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top