To Maintain a Dictionary in Alist Form in C#

I am telling about creation of dictionary which can consist and store multiple information together. It does not involve any SQL table neither any SQL query nor any DB connectivity. It is much more useful for small applications and large applications having small information list.

Complete code showing the keywords and way to maintain a dictionary and perform various processing.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ConsoleApplication1  
  7. {  
  8.     class Program  
  9.     {  
  10.        static void Main(string[] args)  
  11.        {  
  12.           // a dictionary to maintain the authors with their age  
  13.           Dictionary<string, Int16> AuthorList = new Dictionary<string, Int16>();  
  14.           AuthorList.Add("Manish", 35);  
  15.           AuthorList.Add("Mukesh", 25);  
  16.           AuthorList.Add("Praveen", 29);  
  17.           AuthorList.Add("Rajesh", 21);  
  18.           AuthorList.Add("Dinesh", 84);  
  19.           AuthorList.Add("Manisha Gupta", 22);  
  20.   
  21.          // Remove item with key = 'Manisha Gupta'  
  22.          AuthorList.Remove("Manisha Gupta");  
  23.   
  24.         //Find a key  
  25.         if (!AuthorList.ContainsKey("Manisha Gupta"))  
  26.         {  
  27.           AuthorList["Manisha Gupta"] = 22;  
  28.           Console.WriteLine("Key not found");  
  29.           Console.ReadLine();  
  30.        }  
  31.        else  
  32.        {  
  33.           Console.WriteLine("Key found");  
  34.           Console.ReadLine();  
  35.        }  
  36.   
  37.       //Find a value  
  38.       if (!AuthorList.ContainsValue(9))  
  39.       {  
  40.          Console.WriteLine("Item not found");  
  41.          Console.ReadLine();  
  42.      }  
  43.      else  
  44.          Console.WriteLine("Item found");  
  45.          Console.ReadLine();  
  46.   
  47.        //to read all the authors in the dictionary  
  48.        foreach (KeyValuePair<string, Int16> author in AuthorList)  
  49.        {  
  50.           Console.WriteLine("Key: {0}, Value: {1}", author.Key, author.Value);  
  51.           Console.ReadLine();  
  52.       }   
  53.       // to count the number of authors in the authorlist  
  54.       Console.WriteLine("Count: {0}", AuthorList.Count);  
  55.   
  56.      // Set Item value  
  57.      AuthorList["Mukesh"] = 20;  
  58.   
  59.     // Get Item value  
  60.     Int16 age = Convert.ToInt16(AuthorList["Mukesh"]);  
  61.   
  62.     // Get and display keys  
  63.     Dictionary<string, Int16>.KeyCollection keys = AuthorList.Keys;  
  64.     foreach (string key in keys)  
  65.     {  
  66.         Console.WriteLine("Key: {0}", key);  
  67.         Console.ReadLine();  
  68.    }  
  69.   
  70.    // Get and display values  
  71.    Dictionary<string, Int16>.ValueCollection values = AuthorList.Values;  
  72.    foreach (Int16 val in values)  
  73.    {  
  74.       Console.WriteLine("Value: {0}", val);  
  75.       Console.ReadLine();  
  76.    }  
  77. }  
  78.  

you can also change the type of dictionary being created as

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

Dictionary<string,float> AuthorList = new Dictionary<string, float>();

Thus you can easily create the dictionary and use it in your applications.

If you have any query feel free to ask.