Collections-Hashtable - dotnet and db

Follow us on Facebook

SQL Server Mountain View Mountain View Mountain View

Thursday, July 19, 2018

Collections-Hashtable




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 namerequirement
AddAdds an item with a key and value into the hashtable.
RemoveRemoves the item with the specified key from the hashtable.
ClearRemoves all the items from the hashtable.
ContainsChecks whether the hashtable contains a specific key.
ContainsKeyChecks whether the hashtable contains a specific key.
ContainsValueChecks whether the hashtable contains a specific value.
GetHashReturns 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

No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.