ูู ุงูู
ูุงู ุฏูุ ููุชููู
ุนู 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ุงูู ุฑุชุจุท ุจููุง. - ุจูููุฐ ุงูู
IDictionaryinterface.
Differences: ArrayList vs. Hashtable
| Feature | ArrayList | Hashtable |
|---|---|---|
| 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")); // TrueRemoving 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 existOther 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>.