HashSet<T> Constructor (IEnumerable<T>)

Initializes a new instance of the HashSet<T> 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.

Namespace:  System.Collections.Generic
Assemblies:   System.Core (in System.Core.dll)
  System.Collections (in System.Collections.dll)

public HashSet(
	IEnumerable<T> collection
)

Parameters

collection
Type: System.Collections.Generic.IEnumerable<T>

The collection whose elements are copied to the new set.

ExceptionCondition
ArgumentNullException

collection is null.

The capacity of a HashSet<T> object is the number of elements that the object can hold. A HashSet<T> object's capacity automatically increases as elements are added to the object.

If collection contains duplicates, the set will contain one of each unique element. No exception will be thrown. Therefore, the size of the resulting set is not identical to the size of collection.

This constructor is an O(n) operation, where n is the number of elements in the collection parameter.

The following example shows how to create a HashSet<T> collection from an existing set. In this example, two sets are created with even and odd integers, respectively. A third HashSet<T> object is then created from the even integer set.

static void Main()
{
    HashSet<int> evenNumbers = new HashSet<int>();
    HashSet<int> oddNumbers = new HashSet<int>();

    for (int i = 0; i < 5; i++)
    {
        // Populate numbers with just even numbers.
        evenNumbers.Add(i * 2);

        // Populate oddNumbers with just odd numbers.
        oddNumbers.Add((i * 2) + 1);
    }

    Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count);
    DisplaySet(evenNumbers);

    Console.Write("oddNumbers contains {0} elements: ", oddNumbers.Count);
    DisplaySet(oddNumbers);

    // Create a new HashSet populated with even numbers.
    HashSet<int> numbers = new HashSet<int>(evenNumbers);
    Console.WriteLine("numbers UnionWith oddNumbers...");
    numbers.UnionWith(oddNumbers);

    Console.Write("numbers contains {0} elements: ", numbers.Count);
    DisplaySet(numbers);

}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft