Hashtable इंडेक्सर (this[object key]) के माध्यम से कुंजी द्वारा मानों तक पहुंच प्रदान करता है। यदि कुंजी मौजूद नहीं है तो पढ़ने पर null लौटाता है; लिखने पर नई कुंजी/मान जोड़ता है या मौजूदा को अपडेट करता है।
उदाहरण 1: मान प्राप्त करना
using System;
using System.Collections;
public class Demo
{
public static void Main()
{
Hashtable hash = new Hashtable();
hash.Add("1", "AB");
hash.Add("2", "BC");
hash.Add("3", "DE");
hash.Add("4", "EF");
hash.Add("5", "GH");
hash.Add("6", "IJ");
hash.Add("7", "KL");
hash.Add("8", "MN");
hash.Add("9", "OP");
hash.Add("10", "QR");
Console.WriteLine("Value at key 3 = " + hash["3"]);
}
}
आउटपुट
Value at key 3 = DE
उदाहरण 2: मान प्राप्त करना और अपडेट करना
using System;
using System.Collections;
public class Demo
{
public static void Main()
{
Hashtable hash = new Hashtable();
hash.Add("1", "AB");
hash.Add("2", "BC");
hash.Add("3", "DE");
hash.Add("4", "EF");
hash.Add("5", "GH");
hash.Add("6", "IJ");
hash.Add("7", "KL");
hash.Add("8", "MN");
hash.Add("9", "OP");
hash.Add("10", "QR");
Console.WriteLine("Value at key 3 = " + hash["3"]);
hash["3"] = "UV";
Console.WriteLine("Value at key 3 (UPDATED) = " + hash["3"]);
}
}
आउटपुट
Value at key 3 = DE Value at key 3 (UPDATED) = UV
Hashtable कुंजियों के लिए GetHashCode और Equals का उपयोग करता है। इंडेक्सर null कुंजियों की अनुमति नहीं देता (ArgumentNullException फेंकता है)। मान null हो सकते हैं। थ्रेड सुरक्षा के लिए Hashtable.Synchronized का उपयोग करें।