Saturday 5 November 2016

Dictionary

Dictionary

A Dictionary is collection of Key, Value Pairs.

Dictionary Class is Present in  System.Collections.Generic NameSpace.

when creating dictionary we need to specify the type for Key and Value.

Dictionary provides fast lookup for values using keys.

Keys in dictionary must be unique.


Initialize A Dictionary Object In C#:

As Dictionary work with two type of data (one for ‘key’ and one for ‘value’), it requires both these data types to be defined at the time of initialization. Also, the namespace “System.Collections.Generic” is required for using dictionary in your application.
The following snippet will do initialize a simple dictionary with string as both data type:


We can define some default initial values at the time of initialization also. The following examples will add two data pair in the object at the time of initializing:




Add/Remove Pairs In Dictionary Object:


Insert and remove operations are fairly easy with a dictionary object. For adding a new pair, you will need to pass both key and value as parameter in “Add” method on the object. Removing object are done only by the ‘key’ portion. You just need to pass it to object’s “Remove” method. Check the following c# code examples for adding/removing dictionary data pair to object:






Hope this information  helpful to you. Thank you for taking time to look at my blog. ----- Syed Sadiq Ali.      





No comments: