hashtable and dictionary in c# with example

hashtable and dictionary in c# with example
In this article [Show more]

    Hashtable and Dictionary in C# with Example

    In C#, both Hashtable and Dictionary<TKey, TValue> are collections used to store data as key-value pairs. However, they cater to different needs and scenarios. This article explains the differences and similarities between these two types of collections and provides examples to illustrate how to use each effectively.

    Key Differences

    • Generic Type: Dictionary<TKey, TValue> is generic and allows for type-safe storage, which means keys and values can be of any specific type (e.g., string, int). Hashtable, on the other hand, stores keys and values as object, requiring casting when retrieving values and potentially leading to runtime errors.
    • Performance: Dictionary<TKey, TValue> generally offers better performance due to its type safety, which avoids boxing and unboxing operations that Hashtable requires.
    • Thread Safety: Both collections are not thread-safe, but Hashtable provides a synchronized version that can be created using Hashtable.Synchronized.

    Example: Using Hashtable

     

    using System;
    using System.Collections;
    
    public class HashtableExample
    {
        public static void Main()
        {
            Hashtable table = new Hashtable();
            table.Add("id1", "value1");
            table.Add("id2", 2); // Mixed types are allowed but not recommended
    
            // Retrieving an item requires type casting
            string value1 = (string)table["id1"];
            int value2 = (int)table["id2"];
    
            Console.WriteLine("Hashtable:");
            Console.WriteLine($"id1: {value1}");
            Console.WriteLine($"id2: {value2}");
        }
    }
    

    Example: Using Dictionary

     

    using System;
    using System.Collections.Generic;
    
    public class DictionaryExample
    {
        public static void Main()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary.Add("id1", "value1");
            dictionary.Add("id2", "value2");
    
            // No need for casting, type safety ensures you retrieve the correct type
            string value1 = dictionary["id1"];
            string value2 = dictionary["id2"];
    
            Console.WriteLine("Dictionary:");
            Console.WriteLine($"id1: {value1}");
            Console.WriteLine($"id2: {value2}");
        }
    }
    

    When to Use Each

    • Use Dictionary<TKey, TValue>: When you require type safety, better performance, and are working within a .NET environment that supports generics.
    • Use Hashtable: When you are dealing with legacy code that was written before generics were introduced in .NET 2.0, or when you need to store items with weakly-typed keys and values, though this is generally discouraged in modern applications.

    Conclusion

    While Dictionary<TKey, TValue> is generally preferred for new developments due to its type safety and performance benefits, Hashtable is still useful in applications that require integration with older .NET frameworks or when generic types are not available. Understanding these collections and when to use each can help you make better design decisions in your C# applications.


     

    Author Information
    • Author: Ehsan Babaei

    Send Comment



    Comments