Hash Set


using System;
using System.Collections.Generic;

class HashSetDemo
{
    static void Main(string[] args)
    {
        HashSet<int> hashset = new HashSet<int>();   // Declaring a Hash Set

        // Adding values to HashSet.
        hashset.Add(1);
        hashset.Add(2);
        hashset.Add(3);
        hashset.Add(4);

        // For displaying values in a HashSet,we can use a foreach loop
        int limit = hashset.Count;   // Gets the size of HashSet
        Console.WriteLine("Size of hashset: " + limit);
        Console.WriteLine("Using foreach loop");
        foreach (int i in hashset)
        {
            Console.WriteLine(i);
        }

        Console.ReadLine();

    }
}

Output:
Size of hashset: 4
using foreach loop
1
2
3
4