The Hashtable class represents a collection of key-and-value pairs that are organized based on the hash code of the key. The key is used to access the items in the collection. Hashtable.Add(Object, Object) Method is used to adds an element with the specified key and value into the Hashtable.
public virtual void Add(object key, object value);
key: It is the specified key of the element to add of type System.Object.
value: It is the specified value of the element to add of type System.Object. The value can be null.
- ArgumentNullException : If the key is null.
- ArgumentException : If an element with the same key already exists in the Hashtable.
- NotSupportedException : Either Hashtable is read-only or Hashtable has a fixed size.
Below given are some examples to understand the implementation in a better way :
Example 1 :
d: data structures c: c++ q: quiz g: geeks
5: Odd and Prime 9: Odd 2: Even and Prime 4: Even
- C# | Remove the element with the specified key from the Hashtable
- C# | Adding an element to the List
- C# | Check if a Hashtable is equal to another Hashtable
- C# | Get or Set the value associated with specified key in Hashtable
- C# | Hashtable Class
- C# Hashtable with Examples
- C# | Check if the Hashtable contains a specific Value
- C# | Check if the Hashtable contains a specific Key
- C# | Check if Hashtable is read-only
- Difference between Hashtable and Dictionary in C#
- C# | Gets an ICollection containing the keys in the Hashtable
- C# | Gets an ICollection containing the values in the Hashtable
- C# | Check whether a Hashtable contains a specific key or not
- C# | Get an enumerator that iterates through the Hashtable
- C# | Remove all elements from the Hashtable
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.