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

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.