Friday 23 October 2015

Basics of Dictionary

Dictionary


"Dictionary" in C# programming  make use of 'hash table' data-structure to build up the index/ value pair.

Like our normal dictionary it consist of an index and with reference to index it has one more values. (Apni oxford dictionary and c# k dictionary m koe anter nhi h).


Initialize A dictionary Object in C#


Think if you want to make your own dictionary then you must have a name to the dictionary and the type of data which is required to be filled in the dictionary.

Same way,
The syntax goes like

Dictionary <string, string> myDictionary = new Dictionary<string, string>();

Here,

myDictionary is the name of our dictionary,
<string, string> specifies that the index and value will be in string format respectively.

Add Pairs in Dictionary Object :


Now if you want to start to build your dictionary i.e you want to add fields in it.
Then simply we do,

myDictionary.Add("A","Apple");

//Here we have added a index of "A" which is the key and the value assigned to it is Apple.
So, if you refer to "A" then it will return Apple as the value . ( A for Apple).

Delete Pairs in Dictionary :


If you don't like a pair and want to delete it from your dictionary. (tumhri khud ke h kuch bhi kr skta ho).
Then,

myDictionary.Remove("A");

This will delete the index "A" and corresponding values.


Yeah, I think you know more words that start with "A" so, if you want to add more then just one entry per index then make use of List<string> .

The syntax goes as

Dictionary<string, List<string>> myDictionary = new Dictionary<string, List<string>>();

No comments:

Post a Comment