In C# Hashtable is very much similar to disctionary, but the difference is the values that are stored in hashtable are object type. Hashtable follows LIFO (last in first out format). Last inserted value displayed first in the output
Methods of hashkey
| Method name | requirement |
| Add | Adds an item with a key and value into the hashtable. |
| Remove | Removes the item with the specified key from the hashtable. |
| Clear | Removes all the items from the hashtable. |
| Contains | Checks whether the hashtable contains a specific key. |
| ContainsKey | Checks whether the hashtable contains a specific key. |
| ContainsValue | Checks whether the hashtable contains a specific value. |
| GetHash | Returns the hash code for the specified key. |
Example
using System;
using System.Collections;
class classHashTable
{
public void HashtableMethod()
{
Console.WriteLine("Adding Elements to the Hashtable");
Console.WriteLine("=================================");
Hashtable hashtable = new Hashtable();
hashtable.Add(1, "C#");
hashtable.Add(2, "Java");
hashtable.Add(3, "ASP");
hashtable.Add(4, "SQL");
hashtable.Add(5, "MySQL");
hashtable.Add("neo", "Soft");
foreach (DictionaryEntry item in hashtable)
{
Console.WriteLine("Employee ID = " + item.Key + " Employee Name = " + item.Value);
}
Console.WriteLine("\n");
Console.WriteLine("Remove() method removes the item with the specified key from the hashtable.");
Console.WriteLine("=================================");
hashtable.Remove("neo");
foreach (DictionaryEntry item in hashtable)
{
Console.WriteLine("Employee ID = " + item.Key + " Employee Name = " + item.Value);
}
}
}
public class Program
{
public static void Main()
{
classHashTable c = new classHashTable();
c.HashtableMethod();
}
}
Output
Adding Elements to the Hashtable
=================================
Employee ID = neo Employee Name = Soft
Employee ID = 5 Employee Name = MySQL
Employee ID = 4 Employee Name = SQL
Employee ID = 3 Employee Name = ASP
Employee ID = 2 Employee Name = Java
Employee ID = 1 Employee Name = C#
Remove() method removes the item with the specified key from the hashtable.
=================================
Employee ID = 5 Employee Name = MySQL
Employee ID = 4 Employee Name = SQL
Employee ID = 3 Employee Name = ASP
Employee ID = 2 Employee Name = Java
Employee ID = 1 Employee Name = C#
Practice




SQL Server
C#.Net
ASP.Net
ADO.Net
jQuery
No comments:
Post a Comment