Simple Example for HashTable

Susanthampy
Posted by Susanthampy under C# category on | Points: 40 | Views : 2780
In hashtabesclass,

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace HashTable
{
class hashtabes
{
Hashtable ht = new Hashtable();

public string setting()
{

for (int i = 0; i < 5; i++)
{
Console.WriteLine("Enetr the Key");
string key = Console.ReadLine();
Console.WriteLine("Enter the Value");
string value = Console.ReadLine();
ht.Add(key, value);
}
}

public string Getting()
{
Console.WriteLine("Please enter the Key");
string input = Console.ReadLine();
Console.WriteLine(ht[input]);
}


}
}


In Main Program,


using System;
using System.Collections.Generic;
using System.Collections;

using System.Text;

namespace HashTable
{
class Program
{
static void Main(string[] args)
{
hashtabes obj = new hashtabes();
obj.setting();
obj.Getting();

Console.ReadLine();
}
}

}

Comments or Responses

Posted by: Kishork80 on: 5/4/2011 Level:Starter | Status: [Member] | Points: 10
Good code.

Always check for ht.Contains(<key object>) before adding any key into hastable. it will not allow to add duplicate keys and throw exception.
:)
Posted by: Susanthampy on: 5/4/2011 Level:Bronze | Status: [Member] [MVP] | Points: 10
Thank u Kishor
Posted by: CS1401 on: 6/28/2011 Level:Starter | Status: [Member] | Points: 10
Hey susan, its a great job. But its nothing to learn from it. You can write the code can save multiple values in one single key. it can only save only one value corresponding to the one key value.

If you write the code for saving multiple values in single code is a best one to learn.
Thanks..

Login to post response