
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Working with Hashtable and Dictionary in C#
Hashtable
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(); } } }
Output
D04: Marketing D02: HR D03: Operations D01: Finance
Dictionary
Dictionary is a collection of keys and values in C#. Dictionary <TKey, TValue> is included in the System.Collection.Generics namespace.
The following are the methods −
Sr.No. | Methods & Description |
---|---|
1 | Add Add key-value pairs in Dictionary |
2 | Clear() Remove all keays and values |
3 | Remove Removes the element with the specified key. |
4 | ContainsKey Checks whether the specified key exists in Dictionary <TKey, TValue>. |
5 | ContainsValue Checks whether the specified key value exists in Dictionary <TKey, TValue>. |
6 | Count Count the number of key-valu pairs. |
7 | Clear Removes all the elements from Dictionary <TKey, TValue>. |
Let us see how to add elements into a Dictionary and display the count −
Example
using System; using System.Collections.Generic; public class Demo { public static void Main() { IDictionary <int, int> d = new Dictionary <int, int> (); d.Add(1,44); d.Add(2,34); d.Add(3,66); d.Add(4,47); d.Add(5,76); Console.WriteLine(d.Count); } }
- Related Articles
- Difference between Dictionary and Hashtable in C#
- Difference between HashTable and Dictionary in C#
- Hashtable vs. Dictionary in C#
- What is the difference between Dictionary and HashTable in PowerShell?
- How to convert Dictionary to Hashtable in PowerShell?
- Why is a Dictionary preferred over a Hashtable in C#?
- C# Program to Replace Items in One Hashtable with Another Hashtable
- Working with lists and keys in React.js
- Working with Dates and Times in Python
- Working with Dates and Times in R with lubridate
- Python - Working with Pandas and XlsxWriter
- Difference between HashTable and ConcurrentHashMap in Java
- Difference between HashMap and HashTable in Java.
- Differences between HashMap and Hashtable in Java
- Difference between HashTable and HashMap in Java

Advertisements