Loading ...

Introduction of Hashtable

Who is online?  0 guests and 0 members
home  »  articles  »  Introduction of Hashtable

Introduction of Hashtable

(2211)
0
/5
Avg: 0/5: (0 votes)
Published: 6/9/2010 by  Vijendra Shakya

 

Download Project

Hash table is a collection class where objects are stores with key/value pair,

key cannot be null, but value can be null. This comes under the System.Collections namespace.

In Hash table each element is in key/value pair and store in DictionaryEntry objects.

Data type of the hashtable is Object and the default size is 16.

Hashtable implements 15 constructor. Key is use to search the requested data from the Hashtable,

key is immutable and cannot be duplicate but value can be.

Following is the mostly used method of the Hashtable.

ADD(object key,object value):

Adds an element with the specified key and value in the hashtable.

Parameter:

1.key: The key of the element to add.

2.value: The value of the element to add. The value can be null.
Return Value:
Void(no return value)

When we are adding the element in the Hashtable there are 3 type of exceptions comes.

Exception:

1. ArgumentNullException => This error comes when key is null. 

2. ArgumentException => When  an element with the same key already exists in the Hashtable. 

3. NotSupportedException => When The Hashtable is read-only or has a fixed size.

Example:
Hashtable hashtable = new Hashtable();
//Add elements in the Hashtable

hashtable.Add("A", "Vijendra");

hashtable.Add("B", "Singh");

hashtable.Add("C", "Shakya");

hashtable.Add("D", "Delhi");

 

//Fetch data from the hash table

StringBuilder hashOutPut = new StringBuilder();
foreach (DictionaryEntry element in hashtable)

 {

   hashOutPut.AppendFormat("{0} : {1}  ", element.Key, element.Value);

}

Response.Write(hashOutPut);
OUTPUT:
A: Vijendra  B: Singh  C: Shakya  D: Delhi

 

ContainsKey(object key):

Contains method is used to determine whether the Hashtable contains a specific key.

Parameter:

1. key: The key to search in the Hashtable.

Return Value:
return true if the hashtable have the specific key otherwise returns false.

Exception:

ArgumentNullException => When key is null.
Example:
//Find out the hashtable contains the sepcified key

bool keycontains = hashtable.ContainsKey("E");

Response.Write(keycontains);
OUTPUT:
False 

Contains(object key):

Contains method is used to determine whether the Hashtable contains a specific key.

Contains method interanally call the ContainsKey method.

Parameter:

1. key: The key to search in the Hashtable.

Return Value:
return true if the hashtable have the specific key otherwise returns false.

Exception:

ArgumentNullException => When key is null.

Example:
//Find out the hashtable contains the sepcified key

bool keycontains = hashtable.Contains("A");

Response.Write(keycontains);
OUTPUT:
True

   

ContainsValue(object key)

Contains method is used to determine whether the Hashtable contains a specific value.

Parameter:

1.value The value to locate in the Hashtable. The value can be null.

Return Value:
return true if the hashtable have the specific value otherwise returns false.
Example:
//Find out the hashtable contains the sepcified key

bool valuecontains = hashtable.ContainsValue("Vijendra");

Response.Write(valuecontains);
OUTPUT:
True
 

CopyTo(Array array,int arrayIndex):

This method copies Hashtable elements to a one-dimensional Array object at the specified index.

Parameter:

1.array:The one-dimensional Array that is the destination of the DictionaryEntry objects copied from Hashtable. The Array must have zero-based indexing.

2.arrayIndex :The zero-based index in array at which copying begins.
Return Value:
Void(no return value)

Exception:

1.ArgumentNullException => When Array is null. 

2.ArgumentOutOfRangeException => When arrayIndex is less than zero. 

3.ArgumentException ==> This exceptin comes in following cases:

1. When array is multidimensional.

2. arrayIndex is equal to or greater than the length of array.

3. The number of elements in the source Hashtable is greater than the available space from arrayIndex to the end of the destination array. 

4.InvalidCastException => when the type of the source Hashtable cannot be cast automatically to the type of the destination array.
Example:
//For copying the element of the Hashtable we need to create an Array of one dimision

string[] HashElement = new string[hashtable.Count];

hashtable.Values.CopyTo(HashElement, 0);

// Now We can see as follows that data copied or not

StringBuilder arrayCopyTo = new StringBuilder();

foreach (var element in HashElement)

  {

     arrayCopyTo.AppendFormat("{0}</br>", element);

  }

Response.Write(arrayCopyTo);

OUTPUT:
Vijendra
Singh
Shakya
Delhi

Count:

This is the property of the Hashtable to find out the number of key/vlaue pairs available in the hashtable.

Return Value:
The number of key/value pairs available in the Hashtable.
Example:
//Count the element from the Hashtable

int hashCount = hashtable.Count;

Response.Write("Total Elements:" + hashCount);
OUTPUT:

Total Elements:4

 

Remove(object key)

Removes a specific key from the hashtable.

Parameter:

1. key: The key to remove from the Hashtable.

Return Value:
Void(no return value)

Example:

//Remove an element from the Hashtable

hashtable.Remove("C"); 

//Fetch data from the hash table after remove one element

StringBuilder elementAfterRemove = new StringBuilder();

foreach (DictionaryEntry element in hashtable)

{

elementAfterRemove.AppendFormat("{0} : {1}", element.Key, element.Value);

}

Response.Write(elementAfterRemove);
OUTPUT:
A: Vijendra  B: Singh  D: Delhi
//Or we can also test it to find the count of the Hashtable.Now Count should be 3.

Response.Write("Total Elements:" + hashtable.Count);
Total Elements:3

 

IsFixed:

This Hashtable property is used to indicate where the hashtable has fixed size, we can not set the value of the IsFixed.

Return Value:
true if the Hashtable has a fixed size; otherwise, false. The default value of IsFixed is false.

Example:
Response.Write(hashtable.IsFixedSize);

OUTPUT:
False

 

IsReadonly:

This property used to indicate where the hashtable has readoly type,we can not set the value of the IsReadonly.

Return Value:
true if the Hashtable has a readoly; otherwise, false. The default value of IsReadonly is false.

Example:
Response.Write(hashtable.IsReadOnly);
OUTPUT:
False

Clear():

This method removes all the items from the hashtable.

Return Value:
Void(no return value)

Exception:

NotSupportedException => This exception comes when the Hashtable is read-only.

Example:
hashtable.Clear();

int countAfterClear = hashtable.Count;

//now count should be Zero(0) of the hashtable

Response.Write("Total Element: "+countAfterClear);
OUTPUT:
Total Element: 0 


I hope it will help to all asp.net users.
Waiting for your valuable comments Smile.


 

Comments (1)

chandradev

It is nice post. Thank for sharing this post.

7/19/2010
 · 
 
by
  • Name:*
  • Email:*
  • Website:
Type the characters you see in the image *

Confirm

Product Spotlight

ASP.NET Hosting Spotlight

Most Recent Articles

 

Product Spotlight

ASP.NET Hosting Spotlight

Quick Vote

What kind of email newsletter would you prefer to receive from CodeAsp.Net?