Introduction About Dictionary in c# :

  1. Dictionary is collection of (Key, Value) pairs.
  2. Dictionary class is present in System.Collections.Generic namespaces.
  3. When creating Dictionary, we should specify the type for key and value.
  4. Dictionary provides fast lookup for values using keys.
  5. Keys in Dictionary must be unique.
i will Explaining With Example Code . First we Should open Visual Studio and choose Console Application.
Go to File -> New -> Project-> Visual C# -> Console Application.
my console application name called as Dictionary. I have Student Class with Id,Name,Salary auto implemented property. Let's also create Student object for Student Class in main Method . I called as object Student 1 and initialize Property with values.i crate two more Sudent class object .The object must be unique .finally i created Student Dictionary . Dictionary class present in collections.Generic Namespace .
  1. using System.Collections.Generic;
  2. namespace Dictionary
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Student Student1 = new Student()
  9. {
  10. Id = 1,
  11. Name = "Thennarasu",
  12. Salary = 25000
  13. };
  14. Student Student2 = new Student()
  15. {
  16. Id = 2,
  17. Name = "Karthi",
  18. Salary = 35000
  19. };
  20. Student Student3 = new Student()
  21. {
  22. Id = 3,
  23. Name = "Mano",
  24. Salary = 45000
  25. };
  26. Dictionary<int, Student> StudentDictionary = new Dictionary<int, Student>();
  27. StudentDictionary.Add(Student1.Id, Student1);
  28. StudentDictionary.Add(Student2.Id, Student2);
  29. StudentDictionary.Add(Student3.Id, Student3);
  30. }
  31. }
  32. public class Student
  33. {
  34. public int Id { get; set; }
  35. public string Name { get; set; }
  36. public int Salary { get; set; }
  37. }
  38. }
when Create Dictionary we need to Specify datatype key and value.

For key in this Dictionary .i used key value Student ID and value customer . we used To Add Keyword to add objects to Dictionary.we added all objects Student Id and value objects.
  1. Dictionary<int, Student> StudentDictionary = new Dictionary<int, Student>();
  2. StudentDictionary.Add(Student1.Id, Student1);
  3. StudentDictionary.Add(Student2.Id, Student2);
  4. StudentDictionary.Add(Student3.Id, Student3);
If We want to find the value Dictionary Best way to find the value using the Key. for this Dictionary use Key as StudentID .fastest way retrieve Customer object from Dictionary use Key.Here Key is StudentId .we want Details of Student ID 3 .we should pass the 3 value of Key
  1. Student StudentID3 =StudentDictionary[3];
  2. Console.WriteLine("StudentID={0},StudentName={1},StudentSalary={2}", StudentID3.Id,StudentID3.Name,StudentID3.Salary);
  3. Console.ReadLine();
Result Looks like Student Id 3 Details,

Next Going to Display all result using for each loop. we use Keyvaluepair Because Dictionary collections of key and value . foreach loop keyvaluepair we should mention Tkey and Tvalue. in our Code we use Tkey using StudentId and Tvalue using Student.
  1. foreach(KeyValuePair<int,Student> StudentKeyvaluepair in StudentDictionary)
  2. {
  3. Console.WriteLine("Key={0}", StudentKeyvaluepair.Key);
  4. Student stud = StudentKeyvaluepair.Value;
  5. Console.WriteLine("Student Id={0}, Student Name={1},Student Salary={2}", stud.Id, stud.Name, stud.Salary);
  6. Console.ReadKey();
  7. }
Result Will be Like this
Keys in the Dictionary must be unique. in our code already have Student 1 and Student 2 ,Student 3 . Now Trying To add Student 1 object once again .it will show Error ,

Error Showing An Item with same Key has already been Added. How to check Exists or Not in this Dictionary object we use a method called Contains key,
  1. Dictionary<int, Student> StudentDictionary = new Dictionary<int, Student>();
  2. StudentDictionary.Add(Student1.Id, Student1);
  3. StudentDictionary.Add(Student2.Id, Student2);
  4. StudentDictionary.Add(Student3.Id, Student3);
  5. if (!StudentDictionary.ContainsKey(Student1.Id))
  6. {
  7. StudentDictionary.Add(Student1.Id, Student3);
  8. }
Next We Discuss About Following Methods of Dictionary Class,
  1. TryGetvalue()
  2. Remove
  3. Clear()
  4. Count()
TryGetValue

TryGetvalue when we use if not sure Dictionary Contain specific key in this Dictionary. We use tryGetvalue To find The value .when using TrygetValue value comes with Out parameter.
we use key value Student Id .if a key value is found in the Dictionary value is print otherwise key value not Found its Key is not Found
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Dictionary
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Student Student1 = new Student()
  10. {
  11. Id = 1,
  12. Name = "Thennarasu",
  13. Salary = 25000
  14. };
  15. Student Student2 = new Student()
  16. {
  17. Id = 2,
  18. Name = "Karthi",
  19. Salary = 35000
  20. };
  21. Student Student3 = new Student()
  22. {
  23. Id = 3,
  24. Name = "Mano",
  25. Salary = 45000
  26. };
  27. Dictionary<int, Student> StudentDictionary = new Dictionary<int, Student>();
  28. StudentDictionary.Add(Student1.Id, Student1);
  29. StudentDictionary.Add(Student2.Id, Student2);
  30. StudentDictionary.Add(Student3.Id, Student3);
  31. Student Stud;
  32. if(StudentDictionary.TryGetValue(3,out Stud))
  33. {
  34. Console.WriteLine("Id={0},Name={1},Salary={2}", Stud.Id, Stud.Name, Stud.Salary);
  35. Console.ReadLine();
  36. }
  37. else
  38. {
  39. Console.WriteLine("key is not Found");
  40. }
  41. }
  42. }
  43. public class Student
  44. {
  45. public int Id { get; set; }
  46. public string Name { get; set; }
  47. public int Salary { get; set; }
  48. }
  49. }
Result will be like,
Student id 3 available in dictionary returns the ID,Name,Salary all value.otherwise Id hasn't founded function goes for as else part.

Count

if we want count How many items in Dictionary then we use Count Keyword.
  1. Dictionary<int, Student> StudentDictionary = new Dictionary<int, Student>();
  2. StudentDictionary.Add(Student1.Id, Student1);
  3. StudentDictionary.Add(Student2.Id, Student2);
  4. StudentDictionary.Add(Student3.Id, Student3);
  5. Console.WriteLine("Total item of Dictionary ={0}", StudentDictionary.Count);
  6. Console.ReadLine();
Result

Remove

If you want to remove Particular Item remove from Dictionary we use Remove Keyword.
  1. StudentDictionary.Remove(3);

Clear

if you want to remove all item remove from Dictionary we use Clear keyword.

StudentDictionary.Clear();

Conculsion

I hope you all understand in this article What is the Dictionary and How to use Dictionary also dicussed about Four Method TryGetvalue ,Remove ,count and Clear. please share all your feedback to improve my future Article.