Advertisement



< Prev
Next >



C# Hashtable Class



The non-generic Hashtable collection class stores a collection of key-value pairs, where key and value are objects.

The Hashtable uses a mechanism of hashing to store each key-value pair and to perform an association between each key-value pair. In hashing, a unique hash code of each key is computed internally and this value of hash code is used as an index at which its associated value is stored in the Hashtable.

Hashtable collection class implements IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, and ICloneable interfaces and it is defined in the System.Collections namespace.




Some important features of Hashtable







Some commonly used constructors of Hashtable


Constructor Description
Hashtable()
This constructor creates an empty instance of the Hashtable class by using the default initial capacity, hash code provider, and comparer.

The capacity of a Hashtable is the number of elements it can hold, as the new elements are added to the Hashtable, its capacity is automatically increased.

Hashtable(int capacity)
This constructor initializes creates a Hashtable that is empty and has the specified initial capacity.






Properties of Hashtable:


Properties Description
Capacity
This property is used to get or set the total number of elements a Hashtable can hold.

Count
This property gives total number of elements in a Hashtable.

IsFixedSize
This property determines whether a Hashtable has a fixed size or not.

IsReadOnly
This property determines whether a Hashtable is read-only or not.

IsSynchronzied
This property determines whether a Hashtable is thread-safe or not.

Item[Object key]
This property gets or sets a value associated with the specified key in a Hashtable.

To use this property, you should not type Item, but you only need to specify the specific key within a pair of square brackets [ ]

Keys
This property gets an ICollection containing the keys of a Hashtable.

Values
This property gets an ICollection containing the values of a Hashtable.

comparer
This property gets or sets IComparer to use for a Hashtable.






Some important methods of Hashtable.



Methods Description
int Add(Object key, Object value)
This method adds the specified key-value pair in Hashtable.

bool Contains(Object key)
This method checks if Hashtable contains a specific key.

bool ContainsKey(Object key)
This method checks if Hashtable contains a specific key.

bool ContainsValue(Object key)
This method checks if Hashtable contains a specific value.

void Clear()
This method removes all of the elements from the Hashtable.

int GetHash(Object key)
This method returns the hash code for the specified key.

IDictionaryEnumerator GetEnumerator()
This method returns to enumerate the entire Hashtable.

void Remove(Object key)
This method removes the element with the specified key in the Hashtable.






Hashtable example


We will iterate over all the key-value pairs and we will also obtain a value on the basis its key.

//C# Example of Hashtable

using System;
using System.Collections;

class HashtableDemo
{
public static void Main(String[] ar)
{
	//Creating a Hashtable
	Hashtable hm = new Hashtable();

	//Adding key-value pairs by the Add() method
	hm.Add("Max", 1000);
	hm.Add("John", 4000);
	hm.Add("Tom", 2000);
	hm.Add("Ana", 6000);
	hm.Add("Rick", 5000);

	//Getting a collection of the keys using property Keys
	ICollection c = hm.Keys;
	
	// Use the keys to obtain the values.
	foreach(String str in c)
		Console.WriteLine(str + ": " + hm[str]);

	//Using the property Item to retrieve the value of key "Tom"
	//Remember the property Item is not used with its name
	Console.WriteLine("Value at the key, Tom is "+ hm["Tom"]);

	//Using the property Item to retrieve the value of key "Ana"
	Console.WriteLine("Value at the key, Ana is "+ hm["Ana"]);
}
}

Output is :

John: 4000
Max: 1000
Tom: 2000
Ana: 6000
Rick: 5000
Value at the key, Tom is 2000
Value at the key, Ana is 6000


Program Analysis





Advertisement




Checking if a Hashtable contains a specific key or a specific value


Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement