

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the Hashtable class in C#?
Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. It uses the key to access the elements in the collection.
Some of the commonly used methods in Hashtable class are −
Sr.No. | Method & Description |
---|---|
1 | public virtual void Add(object key, object value); Adds an element with the specified key and value into the Hashtable. |
2 | public virtual void Clear(); Removes all elements from the Hashtable. |
3 | public virtual bool ContainsKey(object key); Determines whether the Hashtable contains a specific key. |
4 | public virtual bool ContainsValue(object value); Determines whether the Hashtable contains a specific value. |
The following is an example showing the usage of Hashtable class in C#.
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(); } } }
Above we have used the Hashtable class add() method to add elements with the key and value pair.
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
- Related Questions & Answers
- What is the IsReadOnly property of Hashtable class in C#?
- What is the Keys property of Hashtable class in C#?
- What is the IsFixedSize property of Hashtable class in C#?
- What is the Item property of Hashtable class in C#?
- What is the Count property of Hashtable class in C#?
- What is the Values property of Hashtable class in C#?
- The HashTable Class in Javascript
- What is the differences between HashMap and HashTable in Java
- What is the difference between Dictionary and HashTable in PowerShell?
- What is the class "class" in Java?
- Check if a Hashtable is equal to another Hashtable in C#
- What is the super class of every class in Java?
- What is the Queue class in C#?
- What is the BitArray class in C#?
- What is the Mutex class in C#?
Advertisements