C# | Adding an element into the Hashtable Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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. Syntax: public virtual void Add(object key, object value); Parameters: 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. Exceptions: 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 : CSHARP // C# code for adding an element with the // specified key and value into the Hashtable using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Hashtable Hashtable myTable = new Hashtable(); // Adding elements in Hashtable myTable.Add("g", "geeks"); myTable.Add("c", "c++"); myTable.Add("d", "data structures"); myTable.Add("q", "quiz"); // Get a collection of the keys. ICollection c = myTable.Keys; // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + myTable[str]); } } Output: d: data structures c: c++ q: quiz g: geeks Example 2: CSHARP // C# code for adding an element with the // specified key and value into the Hashtable using System; using System.Collections; class GFG { // Driver code public static void Main() { // Creating a Hashtable Hashtable myTable = new Hashtable(); // Adding elements in Hashtable myTable.Add("4", "Even"); myTable.Add("9", "Odd"); myTable.Add("5", "Odd and Prime"); myTable.Add("2", "Even and Prime"); // Get a collection of the keys. ICollection c = myTable.Keys; // Displaying the contents foreach(string str in c) Console.WriteLine(str + ": " + myTable[str]); } } Output: 5: Odd and Prime 9: Odd 2: Even and Prime 4: Even Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.hashtable.add?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Adding an element into the Hashtable S Sahil_Bansall Follow Improve Article Tags : Misc C# CSharp-method CSharp-Collections-Hashtable CSharp-Collections-Namespace +1 More Practice Tags : Misc Similar Reads C# | Add element to HashSet A HashSet is an unordered collection of the unique elements. It comes under System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. Elements 2 min read C# | Copying the Hashtable elements to an Array Instance Hashtable.CopyTo(Array, Int32) Method is used to copy the elements of a Hashtable to a one-dimensional Array instance at the specified index.Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array : The one-dimensional Array that is the destination of the DictionaryEntry 3 min read C# | Remove all elements from the Hashtable 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.Clear Method is used to remove all elements from the Hashtable. Syntax: myTable.Clear() Here myTable is the name o 2 min read Hashtable elements() Method in Java The elements() method in the Hashtable class in Java returns an enumeration of the values contained in the hashtable. The enumeration returned by this method traverses the hashtable's values in the order in which they were added to the hashtable. The elements() method is inherited from the java.util 2 min read Hashtable get() Method in Java The Hashtable.get() method is a built-in method of the java.util.Hashtable class. This method is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the table contains no such mapping for the key. In this article, we will learn about the Ha 3 min read C# | Number of elements in HashSet A HashSet is an unordered collection of the unique elements. It is found in System.Collections.Generic namespace. It is used in a situation where we want to prevent duplicates from being inserted in the collection. As far as performance is concerned, it is better in comparison to the list. You can u 3 min read Hashtable containsValue() Method in Java The java.util.Hashtable.containsValue() method is used to check whether a particular value is being mapped by a single or more than one key in the Hashtable. It takes the Value as a parameter and returns True if that value is mapped by any of the keys in the table. Syntax: Hash_Table.containsValue(O 2 min read C# | Get an enumerator that iterates through the Hashtable Hashtable.GetEnumerator Method is used to returns an IDictionaryEnumerator that iterates through the Hashtable. Syntax: public virtual System.Collections.IDictionaryEnumerator GetEnumerator (); Return Value: It returns an IDictionaryEnumerator for the Hashtable. Below programs illustrate the use of 2 min read C# | Check whether a Hashtable contains a specific key or not Hashtable.Contains(Object) Method is used to check whether the Hashtable contains a specific key or not. Syntax: public virtual bool Contains (object key); Here, key is the Key of Object type which is to be located in the Hashtable. Return Value: This method returns true if the Hashtable contains an 2 min read Hashtable clone() Method in Java The Hashtable.clone() is a built-in method in the Java Hashtable class. This method is used to create a shallow copy of the existing hashtable. A shallow copy means the keys and values are not cloned themselves, only the structure is duplicated. This method returns a cloned copy of the hashtable wit 2 min read Like