Create HashSet with default equality comparer in CSharp

Description

The following code shows how to create HashSet with default equality comparer.

Example


/*  w w  w.  java2  s . com*/
using System;
using System.Collections.Generic;

public class MainClass{
  public static void Main(String[] argv){  
    HashSet<int> evenNumbers = new HashSet<int>();
    for (int i = 0; i < 5; i++)
    {
        evenNumbers.Add(i * 2);
    }
    evenNumbers.Add(1);
    evenNumbers.Add(2);
    
    foreach(int s in evenNumbers){
       Console.WriteLine(s);
    }
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary