Introduction
Here I am going to discuss about two important method of the Dictionary>tkey, tvalue< class to play with the data of the Dictionary.
Dictionary<tkey, tvalue> class allows us to create key , vlaue pair collection. The Dictionary<tkey, tvalue> generic class maps of keys to a set of values. Dictionary consists of a value and its associated key. Value can be retived by using its key is very fast, close to O(1), because the Dictionary>tkey, tvalue< class is implemented as a hash table.
Now to understand what I am going to discuss in this post I am creating Dictionary object
Dictionary<string, string> dic = new Dictionary<string, string>();
Dictionary Class has method Add which allows to add the key, value pair data in the dictionary object
dic.Add("Pranay", "Rana");
dic.Add("Hemang", "Vyas");
dic.Add("Krunal", "Mevada");
dic.Add("Hanika", "Arora");
Now after adding the key,value pair it's need to take out the value out of the dictionary object and use that values because after all dictionary object used to store the values and to get it back.
To take value back from the Dictionary object , it require to make use of key
Type value = dic[key];But the problem with this is it throws KeyNotFoundException if the key is not exists in dictionary.
ContainsKey
Dictionary class provide methods ContainsKey which used to check where the key is present or not. Signature bool ContainsKey(<TKey> key)
So most of the developer use this method to check value and if the value is exits they get the value back.
Read more: Codeproject
0 comments:
Post a Comment