C# Tutorial for Beginner : Chapter 8 Dictionary

Introduction

Dictionary in C# is same as language dictionary. Each language dictionary is a collection of words and their definitions, regularly recorded one after another in order in at least one particular dialects. Similarly, the Dictionary in C# is a collection of Keys and Values, where key resembles word and value resembles definition. The key is indistinguishable in a key-value match and it can have at most one value in the dictionary, however a value can be related with a wide range of keys.

This class is characterized in the System.Collections.Generic namespace, so you should import or utilizing System.Collections.Generic namespace. Dictionary<TKey, TValue> is a generic collection where TKey means the type of key and TValue is the sort of value. Perhaps you might heard other collection about "List" or "Hashtable" before. Is there the difference between  them ? Of course there is, although "List", "Hashtable", "Dictionary" have the same purpose to store collection of data,  there are some differences between them, whether it is data type that can be used or even performance like data speed retrieval. We will discuss this in the future post.

Dictionary Initialization and Declaration 

Ilustration of dictionary's declaration and initialization
Illustration of dictionary's declaration and initialization
Dictionary<Tkey, Tvalue> variableName = new Dictionary<Tkey, Tvalue>();

In illustration above, I used string for key and value to resemble the uses of dictionary. You can change this pair key and value as you wish. Besides primitive  data type like string, integer, double, float, and so on, you can use reference data types which is your-own-made class. After declaration you can initialize it by add "new" keyword which indicates you create an object of the class followed by "new" keyword. specify the type with declaration otherwise it will give error. 

Adding and Edit Values to Dictionary 

Adding Values in dictionaries
Adding Values in Dictionaries
variableName.Add(Tkey, TValue);

Adding values in dictionaries require object to call "Add(TKey,TValue)" method. Place Tkey with your key object and do like that with TValue. One key only have one value. However multiple keys can store value with same data. Take example picture above, If I add again with Tkey "Love" and Tvalue "Amour2", this code line will give an error said that key "Love" is already defined but it is allowed if I add TValue "Amour" into Tkey "Love2". If we add data, can we edit it too ? Of course you can if you only need to modify value but you can't change the key of an existing dictionary entry. You'll have to remove/add using a new key. To edit value from a specific key, you can use this:
frenchDictionary["Pencil"] = "ulala";

Retrieve Key-Value Pair in Dictionary

We can retrieve values from Dictionary using foreach loop with the help "KeyValuePair<Tkey, TValue>" class. Remember the generics in KeyValuePair and your dictionary have to be same.
foreach (KeyValuePair<string, string> pair in frenchDictionary)
{
    Console.WriteLine(pair.Key.ToString() + "  ==  " + pair.Value.ToString());
}

Search Key and Retrieve Value from Dictionary

There are one or two ways to search key to retrieve value. First, you can use "ContainsKey" method  to search a key in dictionay. This method process the hashcode for its argument and checks the internal structure of your dictionary.
if (frenchDictionary.ContainsKey("Love") == true)
{
 Console.WriteLine("Found It ! "+frenchDictionary["Love"].ToString());
}
else
{
 Console.WriteLine("Key does not exist");
}

Second, by using "TryGetValue" method you not only get boolean value but  also produce a retrieved value. You have to use conditional statement for this. If dictionary can find key you seek , then it will return true and also the value. Why value can be retrieved while this method already return a value ? this is because "out" keyword in argument indicates it also produce value if return true.
if (phoneBook.TryGetValue(inputQuery, out foundedValue))
{
                    Console.WriteLine(inputQuery + "=" + foundedValue);
}
else
{
 Console.WriteLine("Not found");
}

Comments

Popular posts from this blog

Computer Graphics Project "Planet Orbit" Source Code with Glut OpenGL

Quantization of Image Data Implementation in C#

Computer Graphics Project "MINION" Source Code with Glut OpenGL