Collections
C# includes various classes that store the values or objects called collections.
There are two types of collections available in C#.
Non-generic collections and Generic collections
The System. The collections namespace contains the non-generic collection types and systems.Collections.Generic namespace includes generic collection types.
In most cases, it is recommended to use the generic collections because they perform faster than non-generic collections and also minimize exceptions by giving compile-time errors.
Non-generic Collections ArrayList, Stack, Queue, Hashtable
Hashtable
A Hashtable is a collection of key/value pairs. It is the non-generic type of collection that is defined in the System. Collections namespace.
How to create?
syntax
Hashtable hashtable_name = new Hashtable();
Example
// C# program to illustrate how
// to create a hashtable
using System;
using System.Collections;
class GFG {
// Main Method
static public void Main()
{
// Create a hashtable
// Using Hashtable class
Hashtable my_hashtable1 = new Hashtable();
// Adding key/value pair
// in the hashtable
// Using Add() method
my_hashtable1.Add("A1", "Welcome");
my_hashtable1.Add("A2", "to");
my_hashtable1.Add("A3", "India");
Console.WriteLine("Key and Value pairs from my_hashtable1:");
foreach(DictionaryEntry ele1 in my_hashtable1)
{
Console.WriteLine("{0} and {1} ", ele1.Key, ele1.Value);
}
// Create another hashtable
// Using Hashtable class
// and adding key/value pairs
// without using Add method
Hashtable my_hashtable2 = new Hashtable() {
{1, "hello"},
{2, 234},
{3, 230.45},
{4, null}};
Console.WriteLine("Key and Value pairs from my_hashtable2:");
foreach(var ele2 in my_hashtable2.Keys)
{
Console.WriteLine("{0} and {1}", ele2,
my_hashtable2[ele2]);
}
}
}
How do you remove elements?
- Clear: This method is used to remove all the objects from the hashtable.
- Remove: This method is used to remove the element with the specified key from the hashtable.
Example
// C# program to illustrate how
// remove elements from the hashtable
using System;
using System.Collections;
class GFG {
// Main Method
static public void Main()
{
// Create a hashtable
// Using Hashtable class
Hashtable my_hashtable = new Hashtable();
// Adding key/value pair
// in the hashtable
// Using Add() method
my_hashtable.Add("A1", "Welcome");
my_hashtable.Add("A2", "to");
my_hashtable.Add("A3", "India");
// Using remove method
// remove A2 key/value pair
my_hashtable.Remove("A2");
Console.WriteLine("Key and Value pairs :");
foreach(DictionaryEntry ele1 in my_hashtable)
{
Console.WriteLine("{0} and {1} ", ele1.Key, ele1.Value);
}
// Before using Clear method
Console.WriteLine("Total number of elements present"+
" in my_hashtable:{0}", my_hashtable.Count);
my_hashtable.Clear();
// After using Clear method
Console.WriteLine("Total number of elements present in"+
" my_hashtable:{0}", my_hashtable.Count);
}
}
How do I check availability?
- Contains: This method is used to check whether the Hashtable contains a specific key.
- ContainsKey: This method is also used to check whether the Hashtable contains a specific key.
- ContainsValue: This method is used to check whether the Hashtable contains a specific value.
using System;
using System.Collections;
class GFG {
// Main Method
static public void Main()
{
// Create a hashtable
// Using Hashtable class
Hashtable my_hashtable = new Hashtable();
// Adding key/value pair in the hashtable
// Using Add() method
my_hashtable.Add("A1", "Welcome");
my_hashtable.Add("A2", "to");
my_hashtable.Add("A3", "India");
// Determine whether the given
// key present or not
// using Contains method
Console.WriteLine(my_hashtable.Contains("A3"));
Console.WriteLine(my_hashtable.Contains(12));
Console.WriteLine();
// Determine whether the given
// key present or not
// using ContainsKey method
Console.WriteLine(my_hashtable.ContainsKey("A1"));
Console.WriteLine(my_hashtable.ContainsKey(1));
Console.WriteLine();
// Determine whether the given
// value present or not
// using ContainsValue method
Console.WriteLine(my_hashtable.ContainsValue("geeks"));
Console.WriteLine(my_hashtable.ContainsValue("to"));
}
}
Happy Coding!
Thanks.