Console C#.Net Tutorial

Visual Studio.NET IDE

Define C#.NET

C# Comment

C# Variables

C# Data Types

C# Escape Sequence

C# Operators

Format String

Operator Precedence

C# Keywords

Constant Variable

Type Conversion

Flow Control

C# Arrays

C# Character

C# Strings

User-Define Methods

Variable Scope

C# Enumerations

C# Structure

C# Exception Handling

Object Oriented Programming

C# Classes

Constructor & Destructor

C# Inheritance

C# Polymorphism

C# Operator Overloading

C# Method Overriding

C# Interface

Abstract Classes & Methods

Sealed Classes, Methods

C# Properties

C# Indexer

C# Delegates

C# Generics

C# Collection

C# ArrayList

C# Stack

C# Queue

C# HashTable

C# SortedList

Page Stats

Visitor: 130

Dictionary

Dictionary is a collection that store items in a key-pair manner. Each value in the collection is identified by its key and will be unique, Similar to English dictionary where each word (Key) has its corresponding meaning (value). The two common types of Dictionary in the System.Collections namespace are Hash table and SortedList.

Hashtable Class

Hashtable stores items as key-value pairs. A hash table stores the key and its value as an object type.

Hashtable ht = new Hashtable();  
		or
Hashtable ht = new Hashtable(20);
List some important properties of Queue
S.No.PropertyDescription
countReturns the total count of elements in the HashTable.
List some important methods of HashTable
S.No.MethodSyntaxDescription
Add()void Add(object key, object value);Adds an item with a key and value into the hashtable.
Remove()void Remove(object key)Removes the item with the specified key
Contains()bool Contains(object key)Check whether the specified key exists in the Hashtable
ContainsKey()bool ContainsKey(object key)Check whether the specified key exists in the Hashtable
ContainsValue()bool ContainsValue(object value)Checks whether the specified value exists in the Hashtable.
Clear()void Clear()Removes all the items from the hashtable.
Add() method - HashTable

The Add() method adds an item with a key and value into the Hashtable. Key and value can be of any data type. Key cannot be null whereas value can be null. Add() will throw an exception if you try to add a key that already exists in the Hashtable. You can retrive the value of an existing key from the Hashtable using indexer.

Example 1: Add elements in HashTable.

Hashtable ht = newHashtable();
ht.Add(1, 10);
ht.Add(2, 20);
ht.Add(3, 30);
ht.Add("four", "forty");

int intValue1 = (int)ht[2];
int intValue2 = (int)ht[3];
string strValue3 = (string)ht[4];

Console.WriteLine(intValue1);
Console.WriteLine(intValue2);
Console.WriteLine(strValue3);
20
30
forty
key: 1, value: 10
key: 2, value: 20
key: 3, value: 30
key: four, value: forty

Display elements from HashTable: Hashtable elements are key-value pairs stored in DictionaryEntry. So you cast each element in Hashtable to DictionaryEntry. Hashtable has a Keys and a Values property that contain all the keys and values respectively. You can use these properties to get the keys and values.

Example 2: Display elements from HashTable.

Hashtable ht = newHashtable();
ht.Add(1, 10);
ht.Add(2, 20);
ht.Add(3, 30);
ht.Add("four", "forty");

foreach(var key in ht.keys)
{
    Console.WriteLine(key);
}

foreach(var value in ht.Values)
{
    Console.WriteLine(Value);
}

foreach (DictionaryEntry item in ht)
    Console.WriteLine("key:{0}, value:{1}",item.Key, item.Value);
1
2
3
four
10
20
30
forty
key: 1, value: 10
key: 2, value: 20
key: 3, value: 30
key: four, value: forty
Remove() method - HashTable

The Remove() method removes the item with the specified key from the hashtable.

Example 3: Removes element from HashTable.

Hashtable ht = newHashtable();
ht.Add(1, 10);
ht.Add(2, 20);
ht.Add(3, 30);
ht.Add("four", "forty");

Ht.Remove("2");

foreach (DictionaryEntry item in ht)
    Console.WriteLine("key:{0}, value:{1}",item.Key, item.Value);
key: 1, value: 10
key: 3, value: 30
key: four, value: forty
Contains(), ContainsKey(), ContainsValue() - HashTable

Contains() and ContainsKey() check whether the specified key exists in the Hashtable collection. ContainsValue() checks whether the specified value exists in the Hashtable.

Example 4: Check for existing elements.

Hashtable ht = newHashtable();
ht.Add(1, 10);
ht.Add(2, 20);
ht.Add(3, 30);
ht.Add("four", "forty");

ht.Contains(2);// returns true
ht.ContainsKey(2);// returns true
ht.Contains(5); //returns false
ht.ContainsValue("forty"); // returns true
Clear() method - HashTable

Clear() method removes all the key-value pairs in the Hashtable.

Example 5: Removes all the key-value pairs in the Hashtable.

Hashtable ht = newHashtable();
ht.Add(1, 10);
ht.Add(2, 20);
ht.Add(3, 30);
ht.Add("four", "forty");

ht.Clear(); // removes all elements
Console.WriteLine("Total Elements: {0}", ht.Count);
Total Elements: 0
Advertisement
Updated: 10-Sep-21