Hashtable

In this chapter you will learn:

  1. How to use for loop to get all keys from a Hashtable

Elements in Hashtable

Hashtable is a data structure we can add key and value pair to it.

The following code uses foreach statement to loop through all keys in a hashtable.

using System;/* j av a2s .  c  om*/
using System.Collections;

class MainClass
{
    public static void Main()
    {
        Hashtable    hash = new Hashtable();
        hash.Add("A", "1");
        hash.Add("B", "2");
        hash.Add("C", "3");
        hash.Add("D", "4");
        hash.Add("E", "5");
        
        foreach (string firstName in hash.Keys)
        {
            Console.WriteLine("{0} {1}", firstName, hash[firstName]);
        }
    }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to check if a Hashtable contains a value
Home » C# Tutorial » Collections
HashSet
Set operation: Except
Set operator intersect
Remove with condition
HashSet contains
HashSet creation
HashSet removing element
Sub set operations
Hashtable
Hashtable ContainsValue
Add elements to a Hashtable
Hashtable containsKey
Remove from Hashtable
Hashtable to array
Queue
Generic Queue
Stack