Advertisement
Constructor | Description |
---|---|
Hashtable() | This constructor creates an empty instance of the Hashtable class by using the default initial capacity, hash code provider, and comparer. The capacity of a Hashtable is the number of elements it can hold, as the new elements are added to the Hashtable, its capacity is automatically increased. |
Hashtable(int capacity) | This constructor initializes creates a Hashtable that is empty and has the specified initial capacity. |
Properties | Description |
---|---|
Capacity | This property is used to get or set the total number of elements a Hashtable can hold. |
Count | This property gives total number of elements in a Hashtable. |
IsFixedSize | This property determines whether a Hashtable has a fixed size or not. |
IsReadOnly | This property determines whether a Hashtable is read-only or not. |
IsSynchronzied | This property determines whether a Hashtable is thread-safe or not. |
Item[Object key] | This property gets or sets a value associated with the specified key in a Hashtable. To use this property, you should not type Item, but you only need to specify the specific key within a pair of square brackets [ ] |
Keys | This property gets an ICollection containing the keys of a Hashtable. |
Values | This property gets an ICollection containing the values of a Hashtable. |
comparer | This property gets or sets IComparer to use for a Hashtable. |
Methods | Description |
---|---|
int Add(Object key, Object value) | This method adds the specified key-value pair in Hashtable. |
bool Contains(Object key) | This method checks if Hashtable contains a specific key. |
bool ContainsKey(Object key) | This method checks if Hashtable contains a specific key. |
bool ContainsValue(Object key) | This method checks if Hashtable contains a specific value. |
void Clear() | This method removes all of the elements from the Hashtable. |
int GetHash(Object key) | This method returns the hash code for the specified key. |
IDictionaryEnumerator GetEnumerator() | This method returns to enumerate the entire Hashtable. |
void Remove(Object key) | This method removes the element with the specified key in the Hashtable. |
//C# Example of Hashtable
using System;
using System.Collections;
class HashtableDemo
{
public static void Main(String[] ar)
{
//Creating a Hashtable
Hashtable hm = new Hashtable();
//Adding key-value pairs by the Add() method
hm.Add("Max", 1000);
hm.Add("John", 4000);
hm.Add("Tom", 2000);
hm.Add("Ana", 6000);
hm.Add("Rick", 5000);
//Getting a collection of the keys using property Keys
ICollection c = hm.Keys;
// Use the keys to obtain the values.
foreach(String str in c)
Console.WriteLine(str + ": " + hm[str]);
//Using the property Item to retrieve the value of key "Tom"
//Remember the property Item is not used with its name
Console.WriteLine("Value at the key, Tom is "+ hm["Tom"]);
//Using the property Item to retrieve the value of key "Ana"
Console.WriteLine("Value at the key, Ana is "+ hm["Ana"]);
}
}
John: 4000
Max: 1000
Tom: 2000
Ana: 6000
Rick: 5000
Value at the key, Tom is 2000
Value at the key, Ana is 6000
Advertisement
//C# Example of Hashtable
using System;
using System.Collections;
class HashtableDemo
{
public static void Main(String[] ar)
{
//Creating a Hashtable
Hashtable hm = new Hashtable();
//Adding key-value pairs by the Add() method
hm.Add("Max", 1000);
hm.Add("John", 4000);
hm.Add("Tom", 2000);
hm.Add("Ana", 6000);
hm.Add("Rick", 5000);
//Getting a collection of the keys using property Keys
ICollection c = hm.Keys;
// Use the keys to obtain the values.
Console.WriteLine("The contents of Hashtable:");
foreach(String str in c)
Console.WriteLine(str + ": " + hm[str]);
//Calling the method Contains() to see if Hashtable contains key "Tom"
Console.WriteLine("Does our Hashtable contains the key, Tom: "+ hm.Contains("Tom"));
//Calling the method ContainsKey() to see if Hashtable contains key "Ana"
Console.WriteLine("Does our Hashtable contains the key, Tom: "+ hm.ContainsKey("Ana"));
//Calling the method ContainsKey() to see if Hashtable contains key "Jack"
Console.WriteLine("Does our Hashtable contains the key, Jack: "+ hm.ContainsKey("Jack"));
//Calling the method ContainsValue() to see if Hashtable contains value 4000
Console.WriteLine("Does our Hashtable contains the value, 4000: "+ hm.ContainsValue(4000));
//Calling the method ContainsValue() to see if Hashtable contains value 10000
Console.WriteLine("Does our Hashtable contains the value, 10000: "+ hm.ContainsValue(10000));
}
}
The contents of Hashtable:
John: 4000
Max: 1000
Tom: 2000
Ana: 6000
Rick: 5000
Does our Hashtable contains the key, Tom: True
Does our Hashtable contains the key, Tom: True
Does our Hashtable contains the key, Jack: False
Does our Hashtable contains the value, 4000: True
Does our Hashtable contains the value, 10000: False
//C# Example of Hashtable
using System;
using System.Collections;
class HashtableDemo
{
public static void Main(String[] ar)
{
//Creating a Hashtable
Hashtable hm = new Hashtable();
//Adding key-value pairs by the Add() method
//Where, each key is a character and each value is a String.
hm.Add('B', "Banana");
hm.Add('M', "Mango");
hm.Add('G', "Grapes");
hm.Add('A', "Apple");
hm.Add('O',"Orange");
//Getting a collection of the keys using property Keys
ICollection c = hm.Keys;
// Use the keys to obtain the values.
Console.WriteLine("The contents of Hashtable:");
foreach(char ch in c)
Console.WriteLine(ch + ": " + hm[ch]);
//Calling the method Remove() to remove a key 'A' and its associated value
hm.Remove('A');
//Calling the method Remove() to remove a key 'M' and its associated value
hm.Remove('M');
//Printing all elements of Hashtable
Console.WriteLine("The updated contents of Hashtable after removing keys 'A' and 'M' and their values:");
foreach(char ch in c)
Console.WriteLine(ch + ": " + hm[ch]);
}
}
The contents of Hashtable:
A: Apple
G: Grapes
B: Banana
O: Orange
M: Mango
The updated contents of Hashtable after removing keys 'A' and 'M' and their values:
G: Grapes
B: Banana
O: Orange
//C# Key cannot be null
using System;
using System.Collections;
class HashtableDemo
{
public static void Main(String[] ar)
{
//Creating a Hashtable
Hashtable hm = new Hashtable();
//Adding key-value pairs by the Add() method
//Where, each key is a character and each value is a String.
hm.Add('B', "Banana");
hm.Add('M', "Mango");
hm.Add('G', "Grapes");
hm.Add('A', "Apple");
hm.Add('O', "Orange");
hm.Add(null, "no value"); //Trying to store a null as a key
}
}
Unhandled Exception: System.ArgumentNullException: Key cannot be null.
Parameter name: key
at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
at HashtableDemo.Main(String[] ar)
//C# A value can be null in a Hashtable
using System;
using System.Collections;
class HashtableDemo
{
public static void Main(String[] ar)
{
//Creating a Hashtable
Hashtable hm = new Hashtable();
//Adding key-value pairs by the Add() method
//Where, each key is a character and each value is a String.
hm.Add('B', "Banana");
hm.Add('M', "Mango");
hm.Add('G', "Grapes");
hm.Add('A', "Apple");
hm.Add('O', "Orange");
hm.Add('N', null); //Trying to store a null as a key
//Getting a collection of the keys using property Keys
ICollection c = hm.Keys;
//Printing the key-value pairs by using the collection of keys to obtain their values.
Console.WriteLine("The contents of Hashtable:");
foreach(char ch in c)
Console.WriteLine(ch + ": " + hm[ch]);
}
}
The contents of Hashtable:
B: Banana
A: Apple
G: Grapes
O: Orange
N:
M: Mango
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement