Order Now

C# Dictionary & Hashtable

shape image

C# Dictionary & Hashtable

C# Dictionary & Hashtable

C# Dictionary (Fixed Type Key &Value )

C# Dictionary is a collection type in C# that allows developers to store a set of keys and values in a single entity, similar to a real-life dictionary. Unlike arrays, which store values based on their indices, dictionaries store values based on keys. The keys must be unique, but the values can be repeated.

Namespace of Dictionary
using System.Collections.Generic;

Syntax of declaration of Dictionary
Dictionary<KeyType, ValueType> dictName = new Dictionary<KeyType, ValueType>();

Dictionary<int, string> dictName = new Dictionary<int, string>();

Add items to the Dictionary
            dictName.Add(1, "Zakir");
            dictName.Add(2, "Zahid");
            dictName.Add(3, "Rakib");
            dictName.Add(4, "Pranto");

Remove an item from the Dictionary

Iterate over the Dictionary
foreach (var item in dictName)
    Console.WriteLine(item.Key + " " + item.Value);
Search for an item in the Dictionary
            //string myString = "";
            string myString = string.Empty;

            if (dictName.TryGetValue(2, out myString))

dictName.TryGetValue(2, out myString) means Try searching for value by key number 2. If you find it, put it in "myString".

C# Hashtable (Dynamic Type Key & Value )

It's almost like a "dictionary". The difference is that its "key" and "value" are dynamic.

Namespace of Hashtable
using System.Collections;

Syntax of declaration of Hashtable
Hashtable hName = new Hashtable();

Add items to the Hashtable
            hName.Add(1, "Zakir"); //key=int, value=string 
            hName.Add("Zahid", "Production Officer"); // key=string, value=string
            hName.Add("Keya", 16); //key=string, value=int
            hName.Add(3.5, "Marvel"); //key=float, value=string

Remove an item from the Hashtable

Search for an item in the Hashtable
if (hName.Contains("keya"))

Iterate over the Keys
foreach (var item in hName.Keys)

Iterate over the Values
foreach (var item in hName.Values)

Iterate over both Keys & Values
foreach (DictionaryEntry item in hName)
   Console.WriteLine(item.Key +" "+item.Value);

Notice, here data type is DictionaryEntry

The order at the output of the loop is not maintained. This means randomly showing the output. 

In conclusion, C# Dictionary and Hashtable are both collection classes used to store key-value pairs in the .NET framework. However, there are some key differences between the two. Dictionary is a generic class that offers better performance and provides more features compared to Hashtable, such as type safety and built-in methods for sorting and searching. On the other hand, Hashtable is a non-generic class and can store keys and values of any object type, but is slower than Dictionary. It's recommended to use Dictionary whenever possible and only use Hashtable when compatibility with older code is required.

Post a Comment

© Copyright 2023 ZakirDev

Order Form

I will contact you on WhatsApp.

Order now