ููŠ ุงู„ู…ู‚ุงู„ ุฏู‡ุŒ ู‡ู†ุชูƒู„ู… ุนู† HashtableุŒ ูˆู‡ูˆ ูˆุงุญุฏ ู…ู† ุงู„ู€ Non-Generic Collections ุงู„ู…ู‡ู…ุฉ ููŠ C# ูˆุงู„ู„ูŠ ุจูŠุญู„ ู…ุดุงูƒู„ ูƒุจูŠุฑุฉ ูƒุงู†ุช ุจุชูˆุงุฌู‡ู†ุง ู…ุน ุงู„ู€ Array ูˆุงู„ู€ ArrayList.

The Problem with Array and ArrayList

ููŠ ุงู„ู€ Array ูˆุงู„ู€ ArrayListุŒ ูƒู†ุง ุจู†ูˆุตู„ ู„ู„ุนู†ุงุตุฑ ุนู† ุทุฑูŠู‚ ุงู„ู€ index ุจุชุงุนู‡ุง. ู„ูƒู† ููŠ ุงู„ุญู‚ูŠู‚ุฉุŒ ุตุนุจ ุฌุฏู‹ุง ู†ูุชูƒุฑ ุงู„ู€ index ุจุชุงุน ูƒู„ ู…ุนู„ูˆู…ุฉุŒ ุฎุตูˆุตู‹ุง ู„ูˆ ุจู†ุชุนุงู…ู„ ู…ุน ุจูŠุงู†ุงุช ู…ุนู‚ุฏุฉ.

ุชุฎูŠู„ ู…ุซู„ู‹ุง ุฅู† ุนู†ุฏูƒ ArrayList ููŠู‡ ุจูŠุงู†ุงุช ู…ูˆุธู ุจุงู„ุชุฑุชูŠุจ ุฏู‡: IDุŒ ุงู„ุงุณู…ุŒ ุงู„ูˆุธูŠูุฉุŒ ุงู„ู…ุฑุชุจุŒ ุงู„ุนู†ูˆุงู†ุŒ ุงู„ู‚ุณู…ุŒ ุงู„ุฅูŠู…ูŠู„.

ArrayList employeeData = new ArrayList();
employeeData.Add(1001);       // EId - Index 0
employeeData.Add("James");    // Name - Index 1
employeeData.Add("Manager");  // Job - Index 2
employeeData.Add(3500);       // Salary - Index 3
employeeData.Add("Mumbai");   // Location - Index 4
employeeData.Add("IT");       // Dept - Index 5
employeeData.Add("a@a.com");  // Emailid - Index 6
 
// To get the Location, I have to remember it's at index 4
Console.WriteLine("Location : " + employeeData[4]);
 
// To get the Email ID, I have to remember it's at index 6
Console.WriteLine("Emaild ID : " + employeeData[6]);

ู„ูˆ ุงู„ุจูŠุงู†ุงุช ูƒุชุฑุชุŒ ุงู„ู…ูˆุถูˆุน ู‡ูŠูƒูˆู† ู…ุณุชุญูŠู„. ู‡ู†ุง ุจูŠุฌูŠ ุฏูˆุฑ ุงู„ู€ Hashtable ุงู„ู„ูŠ ุจูŠุฎู„ูŠู†ุง ู†ูˆุตู„ ู„ู„ุจูŠุงู†ุงุช ุนู† ุทุฑูŠู‚ โ€œู…ูุชุงุญโ€ (key) ุฅุญู†ุง ุงู„ู„ูŠ ุจู†ุญุฏุฏู‡.

What is a Hashtable?

ุงู„ู€ Hashtable ู‡ูˆ Non-Generic Collection ู…ูˆุฌูˆุฏ ููŠ System.CollectionsุŒ ุจูŠุฎุฒู† ุงู„ุจูŠุงู†ุงุช ุนู„ู‰ ุดูƒู„ Key-Value Pairs (ุฃุฒูˆุงุฌ ู…ู† ุงู„ู…ูุชุงุญ ูˆุงู„ู‚ูŠู…ุฉ).

How Does it Work?

ู„ู…ุง ุจุชุถูŠู ุนู†ุตุฑ ููŠ HashtableุŒ ู‡ูˆ ุจูŠุงุฎุฏ ุงู„ู€ key ุงู„ู„ูŠ ุฅู†ุช ุญุฏุฏุชู‡ (ุณูˆุงุก ูƒุงู† string, int, ุฃูˆ ุฃูŠ ู†ูˆุน ุชุงู†ูŠ)ุŒ ูˆุจูŠุนู…ู„ู‡ ุนู…ู„ูŠุฉ hashing ุนุดุงู† ูŠุญูˆู„ู‡ ู„ู€ hash code (ุฑู‚ู… ุตุญูŠุญ ูุฑูŠุฏ). ุจุนุฏูŠู† ุจูŠุณุชุฎุฏู… ุงู„ู€ hash code ุฏู‡ ุนุดุงู† ูŠุฎุฒู† ุงู„ู‚ูŠู…ุฉ ูˆูŠู„ุงู‚ูŠู‡ุง ุจุณุฑุนุฉ ุฌุฏู‹ุง ู„ู…ุง ุชูŠุฌูŠ ุชุทู„ุจู‡ุง ุชุงู†ูŠ.

graph TD
    A["Key (e.g., 'Name')"] -- Hashing Function --> B(Integer Hash Code);
    B -- Used as index --> C{Buckets};
    C -- Stores --> D["Value (e.g., 'James')"];

ู…ู„ุญูˆุธุฉ: ุนู…ู„ูŠุฉ ุงู„ู€ hashing ุฏูŠ ุจุชุฎู„ูŠ ุฃุฏุงุก ุงู„ู€ Hashtable ุฃุจุทุฃ ุดูˆูŠุฉ ู…ู† ุงู„ู€ ArrayListุŒ ู„ุฃู†ู‡ ุจูŠุนู…ู„ ุฎุทูˆุฉ ุฅุถุงููŠุฉุŒ ู„ูƒู†ู‡ ุจูŠูˆูุฑ ู…ุฑูˆู†ุฉ ูˆุณุฑุนุฉ ูƒุจูŠุฑุฉ ููŠ ุงู„ุจุญุซ.

Hashtable Characteristics

  • ุจูŠุฎุฒู† ุงู„ุจูŠุงู†ุงุช ูƒู€ Key-Value Pairs.
  • ุบูŠุฑ ุขู…ู† ู…ู† ู†ุงุญูŠุฉ ุงู„ู†ูˆุน (Non-Generic)ุŒ ูˆู…ูˆุฌูˆุฏ ููŠ System.Collections.
  • ุงู„ู€ Keys ู„ุงุฒู… ุชูƒูˆู† ูุฑูŠุฏุฉ ูˆู…ูŠู†ูุนุด ุชูƒูˆู† null.
  • ุงู„ู€ Values ู…ู…ูƒู† ุชูƒูˆู† null ูˆู…ู…ูƒู† ุชูƒูˆู† ู…ูƒุฑุฑุฉ.
  • ุงู„ูˆุตูˆู„ ู„ู„ู‚ูŠู… ุจูŠุชู… ุนู† ุทุฑูŠู‚ ุงู„ู€ Key ุงู„ู…ุฑุชุจุท ุจูŠู‡ุง.
  • ุจูŠู†ูุฐ ุงู„ู€ IDictionary interface.

Differences: ArrayList vs. Hashtable

FeatureArrayListHashtable
LookupุจูŠุชู… ุนู† ุทุฑูŠู‚ ุงู„ู€ index ุงู„ุฏุงุฎู„ูŠ ูู‚ุท.ุจูŠุชู… ุนู† ุทุฑูŠู‚ key ุฅู†ุช ุงู„ู„ูŠ ุจุชุญุฏุฏู‡.
Performanceุฃุณุฑุน ููŠ ุงู„ุฅุถุงูุฉ ูˆุงู„ุชุตูุญ ุงู„ู…ุชุณู„ุณู„.ุฃุจุทุฃ ู†ุณุจูŠู‹ุง ููŠ ุงู„ุฅุถุงูุฉ ุจุณุจุจ ุงู„ู€ hashingุŒ ู„ูƒู† ุฃุณุฑุน ุฌุฏู‹ุง ููŠ ุงู„ุจุญุซ ุนู† ุนู†ุตุฑ ู…ุนูŠู†.
Scenarioู…ู†ุงุณุจ ู„ูˆ ุนุงูŠุฒ ุชุถูŠู ุนู†ุงุตุฑ ูˆุชู„ู ุนู„ูŠู‡ุง ุจุงู„ุชุฑุชูŠุจ.ู…ู†ุงุณุจ ู„ูˆ ู…ุญุชุงุฌ ุชูˆุตู„ ู„ุจูŠุงู†ุงุช ู…ุนูŠู†ุฉ ุจุณุฑุนุฉ ุนู† ุทุฑูŠู‚ ู…ูุชุงุญ.

Creating a Hashtable

ุฃูˆู„ ุญุงุฌุฉุŒ ู„ุงุฒู… ู†ุนู…ู„ using System.Collections;. ุจุนุฏูŠู† ุจู†ู†ุดุฆ instance ู…ู† ุงู„ู€ Hashtable.

// Create a Hashtable using the default constructor
Hashtable hashtable = new Hashtable();

Adding Elements

ุจู†ุณุชุฎุฏู… Add(key, value) ุนุดุงู† ู†ุถูŠู ุนู†ุงุตุฑุŒ ุฃูˆ ู…ู…ูƒู† ู†ุณุชุฎุฏู… ุทุฑูŠู‚ุฉ ุงู„ู€ collection-initializer.

// Method 1: Using the Add() method
hashtable.Add("EId", 1001);
hashtable.Add("Name", "James");
hashtable.Add("Salary", 3500);
 
// Method 2: Using collection-initializer syntax
var cities = new Hashtable()
{
    {"UK", "London, Manchester, Birmingham"},
    {"USA", "Chicago, New York, Washington"},
    {"India", "Mumbai, Delhi, BBSR"}
};

Accessing Elements

1. Using Keys

ุจู†ูˆุตู„ ู„ู„ู‚ูŠู…ุฉ ู…ุจุงุดุฑุฉ ุนู† ุทุฑูŠู‚ ุงู„ู€ key ุจุชุงุนู‡ุง ุจุงุณุชุฎุฏุงู… ุงู„ู€ indexer [].

Console.WriteLine("Location : " + hashtable["Location"]);
Console.WriteLine("Emaild ID : " + hashtable["EmailID"]);

2. Using a foreach Loop

ูƒู„ ุนู†ุตุฑ ุฌูˆู‡ Hashtable ุจูŠูƒูˆู† ู…ู† ู†ูˆุน DictionaryEntryุŒ ูˆุงู„ู†ูˆุน ุฏู‡ ููŠู‡ property ุงุณู…ู‡ุง Key ูˆูˆุงุญุฏุฉ ุชุงู†ูŠุฉ ุงุณู…ู‡ุง Value.

foreach (DictionaryEntry item in hashtable)
{
    Console.WriteLine($"Key: {item.Key}, Value: {item.Value}");
}
 
// Another way is to loop over the keys
foreach (object key in hashtable.Keys)
{
    Console.WriteLine(key + " : " + hashtable[key]);
}

Checking for Keys and Values

  • ุงู„ู€ Contains(key) ุฃูˆ ContainsKey(key): ุจุชุชุฃูƒุฏ ู„ูˆ ููŠู‡ key ู…ุนูŠู† ู…ูˆุฌูˆุฏ.
  • ุงู„ู€ ContainsValue(value): ุจุชุชุฃูƒุฏ ู„ูˆ ููŠู‡ value ู…ุนูŠู†ุฉ ู…ูˆุฌูˆุฏุฉ.
Console.WriteLine("Is EmailID Key Exists: " + hashtable.ContainsKey("EmailID")); // True
Console.WriteLine("Is Mumbai value Exists: " + hashtable.ContainsValue("Mumbai")); // True

Removing Elements

  • ุงู„ู€ Remove(key): ุจุชุญุฐู ุงู„ุนู†ุตุฑ ุจุงู„ู€ key ุจุชุงุนู‡. ู„ูˆ ุงู„ู€ key ู…ุด ู…ูˆุฌูˆุฏุŒ ู‡ูŠุญุตู„ ExceptionุŒ ุนุดุงู† ูƒุฏุฉ ูŠูุถู„ ุชุชุฃูƒุฏ ุงู„ุฃูˆู„ ุจุงุณุชุฎุฏุงู… ContainsKey().
  • ุงู„ู€ Clear(): ุจุชุญุฐู ูƒู„ ุงู„ุนู†ุงุตุฑ.
// Check before removing to avoid exceptions
if (hashtable.ContainsKey("City"))
{
    hashtable.Remove("City");
}
 
// Remove all elements
hashtable.Clear();

Updating and Assigning with Indexer

ุงู„ู€ indexer [] ู…ุด ุจุณ ู„ู„ูˆุตูˆู„ ู„ู„ู‚ูŠู…ุŒ ุฏู‡ ูƒู…ุงู† ุจู†ุณุชุฎุฏู…ู‡ ุนุดุงู† ู†ุนุฏู„ ู‚ูŠู…ุฉ ู…ูˆุฌูˆุฏุฉ ุฃูˆ ู†ุถูŠู ู‚ูŠู…ุฉ ุฌุฏูŠุฏุฉ ู„ูˆ ุงู„ู€ key ู…ุด ู…ูˆุฌูˆุฏ.

Hashtable employee = new Hashtable { { "Name", "James" } };
 
// Update existing value
employee["Name"] = "Smith";
 
// Add a new key-value pair
employee["Job"] = "Developer"; // This works because the "Job" key doesn't exist

Other Useful Methods and Properties

  • ุงู„ู€ Clone(): ุจุชุนู…ู„ ู†ุณุฎุฉ shallow copy ู…ู† ุงู„ู€ Hashtable.
  • ุงู„ู€ CopyTo(array, index): ุจุชู†ุณุฎ ุนู†ุงุตุฑ ุงู„ู€ Hashtable (ุงู„ู„ูŠ ู‡ูŠ DictionaryEntry) ู„ู€ array. ู…ู…ูƒู† ูƒู…ุงู† ุชู†ุณุฎ ุงู„ู€ Keys ุจุณ ุฃูˆ ุงู„ู€ Values ุจุณ.
    hashtable.Keys.CopyTo(myKeysArray, 0);
    hashtable.Values.CopyTo(myValuesArray, 0);
  • Properties:
    • ุงู„ู€ Count: ุจุชุฑุฌุน ุนุฏุฏ ุงู„ุนู†ุงุตุฑ.
    • ุงู„ู€ Keys: ุจุชุฑุฌุน collection ููŠู‡ุง ูƒู„ ุงู„ู€ keys.
    • ุงู„ู€ Values: ุจุชุฑุฌุน collection ููŠู‡ุง ูƒู„ ุงู„ู€ values.
    • ุงู„ู€ IsReadOnly / IsFixedSize: ุจุชุนุฑููƒ ุฅุฐุง ูƒุงู†ุช ุงู„ู€ collection ู„ู„ู‚ุฑุงุกุฉ ูู‚ุท ุฃูˆ ุญุฌู…ู‡ุง ุซุงุจุช.

ู†ุตูŠุญุฉ: ุงู„ู€ Hashtable ู‡ูˆ non-genericุŒ ูˆู„ู„ุฃูƒูˆุงุฏ ุงู„ุญุฏูŠุซุฉ ุงู„ู„ูŠ ุจุชู‡ุชู… ุจุงู„ู€ type-safety ูˆุงู„ุฃุฏุงุกุŒ ูŠููุถู„ ุงุณุชุฎุฏุงู… ุงู„ุจุฏูŠู„ ุงู„ู€ generic ูˆู‡ูˆ Dictionary<TKey, TValue>.