Dictionary Generic collection class in C#

  • Dictionary class in C# is a generic type which stores objects in a Key-Value format.
  • We can store objects of TValue type as value with TKey type as its Key.
  • Retrieval of value using key is very fast.
  • Every key in dictionary must be unique.
  • A Key cannot be null, but a value can be null, if a type of value is reference type.
  • While enumerating dictionary elements using foreach loop, each element in the dictionary is treated as a KeyValuePair<Tkey,Tvalue> representing a value and its key.
See below code of Using Dictionary class with its basic functions.

+
Code
using System;
using System.Collections.Generic;
 
namespace Dictionary_generic_class
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<intstring> dictEmployees = new Dictionary<intstring>();
 
            Employee emp1 = new Employee() { empId = 111, empName = "Sam" };
            Employee emp2 = new Employee() { empId = 112, empName = "Jane" };
            Employee emp3 = new Employee() { empId = 113, empName = "Sachin" };
            Employee emp4 = new Employee() { empId = 114, empName = "Jack" };
 
            //Add function
            dictEmployees.Add(emp1.empId, emp1.empName);
            dictEmployees.Add(emp2.empId, emp2.empName);
            dictEmployees.Add(emp3.empId, emp3.empName);
            dictEmployees.Add(emp4.empId, emp4.empName);
 
            Console.WriteLine("Looping through dictionary\n");
 
            foreach (KeyValuePair<intstring> kvp in dictEmployees)
            {
                Console.WriteLine("Employee Id:{0}\t\tEmployee Name:{1}",kvp.Key,kvp.Value);
            }
 
            //Remove function
            dictEmployees.Remove(1115);
 
            Console.WriteLine("\nAfter removing element with key 111\n");
 
            foreach (KeyValuePair<intstring> kvp in dictEmployees)
            {
                Console.WriteLine("Employee Id:{0}\t\tEmployee Name:{1}", kvp.Key, kvp.Value);
            }
 
            //TryGetValue function
            string EmployeeWithId = "N/A";
            dictEmployees.TryGetValue(115, out EmployeeWithId);
 
            Console.WriteLine("\nEmployee Name with Id 115 is {0}", EmployeeWithId);
 
            dictEmployees.TryGetValue(112, out EmployeeWithId);
 
            Console.WriteLine("\nEmployee Name with Id 112 is {0}", EmployeeWithId);
 
            //ContainsKey and ContainsValue function
            if (dictEmployees.ContainsKey(113))
                Console.WriteLine("\nEmployee with Id 113 present");
            else
                Console.WriteLine("\nEmployee with Id 113 not present");
 
            if (dictEmployees.ContainsValue("Jack"))
                Console.WriteLine("\nEmployee Jack present");
            else
                Console.WriteLine("\nEmployee Jack not present");
           
            Console.ReadLine();
        }
    }
 
 
    public class Employee
    {
        public int empId { getset; }
        public string empName { getset; }
    }
}

In above code sample, we have created Dictionary class object dictEmployees with key as integer and value as string. We are storing several Employee objects into the dictionary with key as EmpId and value as EmpName.
Below are the basic methods of dictionary, with their use, that we used in our code sample.

Add : This method is used to add object with Key and value of type Tkey and TValue.

Remove : This method removes element from dictionary with specified Key which is passed as parameter for this function.

TryGetValue : This method checks if the provided key as first parameter is present in the dictionary or not. If present. then it outputs corresponding value to a string variable that we passed as a second parameter to this function. If no value is present corresponding to that key then by default it will assign a default value.

ContainsKey/ContainsValue : This method check if a specified Key/Value present in the dictionary or not.

Below is the output of above code:

Dictionary generic class in c#

No comments :