Hashtable Class, Key-and-Value Pairs के Collection को Represent करती है जो Key के Hash Code के आधार पर Organized होते हैं। यह Collection में Elements Access करने के लिए Key का उपयोग करती है।
Hashtable Class में आमतौर पर उपयोग होने वाले कुछ Methods हैं:
| Sr.No. | Method & Description |
|---|---|
| 1 | public virtual void Add(object key, object value); Specified Key और Value के साथ Element को Hashtable में Add करता है। |
| 2 | public virtual void Clear(); Hashtable से सभी Elements Remove करता है। |
| 3 | public virtual bool ContainsKey(object key); Determine करता है कि Hashtable में Specific Key है या नहीं। |
| 4 | public virtual bool ContainsValue(object value); Determine करता है कि Hashtable में Specific Value है या नहीं। |
निम्न Example C# में Hashtable Class के उपयोग को दर्शाता है।
Example
using System; using System.Collections; namespace Demo { class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add("D01", "Finance"); ht.Add("D02", "HR"); ht.Add("D03", "Operations"); if (ht.ContainsValue("Marketing")) { Console.WriteLine("This department name is already in the list"); } else { ht.Add("D04", "Marketing"); } ICollection key = ht.Keys; foreach (string k in key) { Console.WriteLine(k + ": " + ht[k]); } Console.ReadKey(); } } }ऊपर हमने Hashtable Class Add() Method का उपयोग Key और Value Pair के साथ Elements Add करने के लिए किया है।
Hashtable ht = new Hashtable(); ht.Add("D01", "Finance"); ht.Add("D02", "HR"); ht.Add("DO3", "Operations");Output
D04: Marketing D02: HR D03: Operations D01: Finance