C# HashSet HashSet(IEnumerable)

Description

HashSet HashSet (IEnumerable ) initializes a new instance of the HashSet class that uses the default equality comparer for the set type, contains elements copied from the specified collection, and has sufficient capacity to accommodate the number of elements copied.

Syntax


public HashSet(
  IEnumerable<T> collection
)

Parameters

  • collection - The collection whose elements are copied to the new set.

Example

The following example shows how to create a HashSet collection from an existing set.


/*from www  .  j a  v a  2s. com*/
using System;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  
    HashSet<int> evenNumbers = new HashSet<int>();
    HashSet<int> oddNumbers = new HashSet<int>();

    for (int i = 0; i < 5; i++)
    {
        evenNumbers.Add(i * 2);
        oddNumbers.Add((i * 2) + 1);
    }
    HashSet<int> numbers = new HashSet<int>(evenNumbers);

  }
}
    




















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack