C# - The Hashtable Class

The Hashtable class provides a number of properties and methods that you use when working with a hastable. These properties and methods are listed below.

Properties and Methods of the Hashtable Class

Item Description
Properties
Count Contains the number of items currently in the hashtable
IsReadOnly Contains True if the hashtable is read-only and False otherwise
IsSynchronized Contains True if the hashtable is synchronized and False otherwise
Item[] Contains an index of all the items in the hashtable
Keys Contains a collection of all the keys in the hashtable
Values Contains a collection of all the values in the hashtable
Methods
Add() Adds an item to the hashtable
Clear() Clear all items from the hashtable
Clone() Returns a duplicate of the hashtable
Contains() Returns True if a given object is in the current hashtable
ContainsKey() Returns True if a given key is in the current hashtable
ContainsValue() Returns True if a given value is in the current hashtable
CopyTo() Copies all or part of the current hashtable to another hashtable that is passed in as an argument
Remove() Removes a given item from the hashtable
Synchronized() Synchronizes the hashtable
Hashtable.Count

Syntax

Int32 Count

Description

The Hashtable.Count property contains the total number of objects in the hashtable.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");


MyLabel.Text = "Count is "+h.Count.ToString();
Hashtable.IsReadOnly

Syntax

Boolean IsReadOnly

Description

The Hashtable.IsReadOnly property enables a developer to check the read-only status of a hashtable.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");


MyLabel.Text = "Is ReadOnly set to " + h.IsReadOnly;
Hashtable.IsSynchronized

Syntax

Boolean IsSynchronized

Description

The Hashtable.IsSynchronized property enables a developer to see if a given hashtable is synchronized.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");

MyLabel.Text = "IsSynchronized set to " + h.IsSynchronized;
Hashtable.Item

Syntax

Object Item(Object key)

Description

The Hashtable.Item property enables a developer to access individual elements of a hashtable.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");

MyLabel.Text = "Second item in hashtable: " + (string)h["key2"];
Hashtable.Keys

Syntax

ICollection Keys

Description

The Hashtable.Keys property contains a collection of all keys in a hashtable.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");

MyList.DataSource = h.Keys;
MyList.DataBind();
Hashtable.Values

Syntax

ICollection Values

Description

The Hashtable.Values property contains a collection of all values in a hashtable.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");

MyList.DataSource = h.Values;
MyList.DataBind();
Hashtable.Add()

Syntax

Viod Add(Object key, Object value)

Description

The Hashtable.Add() method adds name/value pairs to the hashtable.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");
Hashtable.Clear()

Syntax

Void Clear()

Description

The Hashtable.Clear() method clears all keys and values from the hashtable.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");

h.Clear();
Hashtable.Clone()

Syntax

Object Clone()

Description

The Hashtable.Clone() method creates an identical new hashtable.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");

Hashtable h2 = (Hashtable)h.Clone();
Hashtable.Contains()

Syntax

Boolean Contains(Object key)

Description

The Hashtable.Contains() method enables a developer to check for the existence of a particular key in a hashtable.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");

MyLabel.Text = "Does hashtable contain this key: " + h.Contains("key1").ToString();
Hashtable.ContainsKey()

Syntax

Boolean ContainsKey(Object key)

Description

The Hashtable.ContainsKey() method enables a developer to check for the existence of a particular key in a hashtable.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");

MyLabel.Text = "Does hashtable contain this key: "+h.ContainsKey("key2").ToString();
Hashtable.ContainsValue()

Syntax

Boolean ContainsValue(Object value)

Description

The Hashtable.ContainsValue() method enables a developer to check for the existence of a particular value in a hashtable.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");

MyLabel.Text = "Does hashtable contain this value: "+h.ContainsValue("value1").ToString();
Hashtable.CopyTo()

Syntax

Void CopyTo(Array array, Int32 arrayIndex)

Description

The Hashtable.CopyTo() method copies the hashtable to an array.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");

String[] MyArray = new String[3];
h.CopyTo(MyArray, 0);
MyLabel.Text = "Third item of ArrayList is "+MyArray[2];
Hashtable.Remove()

Syntax

Void Remove( Object key )

Description

The Hashtable.Remove() method removes an entry from the hashtable by key.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");

h.Remove("key3");
Hastable.Synchronized()

Syntax

Hashtable Synchronized( Hashtable table )

Description

The Hashtable.Synchronized() method returns a synchronized duplicate of the current hashtable.

Example

Hashtable h = new Hashtable();
h.Add("key1", "value1");
h.Add("key2", "value2");
h.Add("key3", "value3");

h = Hashtable.Synchronized(h);
MyLabel.Text = "True if items is synchronized: "+h.IsSynchronized;
Write a review
Message:
Rating:
Name:
Email:
(optional)
(Your email that entered here will not show anywhere on website and will use only reference when someone reply your post)
  I want to receive a copy of email on this post (your email is needed)
 
Code: