HashSet<T> Constructor (IEnumerable<T>, IEqualityComparer<T>)

Initializes a new instance of the HashSet<T> class that uses the specified 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
Assembly:  System.Core (in System.Core.dll)

public HashSet(
	IEnumerable<T> collection,
	IEqualityComparer<T> comparer
)

Parameters

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

The collection whose elements are copied to the new set.

comparer
Type: System.Collections.Generic.IEqualityComparer<T>

The IEqualityComparer<T> implementation to use when comparing values in the set, or null to use the default EqualityComparer<T> implementation for the set type.

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 uses a supplied IEqualityComparer<T> to allow case-insensitive comparisons on the elements of a HashSet<T> collection of vehicle types.

using System;
using System.Collections.Generic;

class Program
{
    public static void Main()
    {
        SameVehicleComparer compareVehicles = new SameVehicleComparer();
        HashSet<string> allVehicles = new HashSet<string>(compareVehicles);
        List<string> someVehicles = new List<string>();

        someVehicles.Add("Planes");
        someVehicles.Add("Trains");
        someVehicles.Add("Automobiles");

        // Add in the vehicles contained in the someVehicles list.
        allVehicles.UnionWith(someVehicles);

        Console.WriteLine("The current HashSet contains:\n");
        foreach (string vehicle in allVehicles)
        {
            Console.WriteLine(vehicle);
        }

        allVehicles.Add("Ships");
        allVehicles.Add("Motorcycles");
        allVehicles.Add("Rockets");
        allVehicles.Add("Helicopters");
        allVehicles.Add("Submarines");

        Console.WriteLine("\nThe updated HashSet contains:\n");
        foreach (string vehicle in allVehicles)
        {
            Console.WriteLine(vehicle);
        }

        // Verify that the 'All Vehicles' set contains at least the vehicles in 
        // the 'Some Vehicles' list. 
        if (allVehicles.IsSupersetOf(someVehicles))
        {
            Console.Write("\nThe 'All' vehicles set contains everything in ");
            Console.WriteLine("'Some' vechicles list.");
        }

        // Check for Rockets. Here the SameVehicleComparer will compare 
        // true for the mixed-case vehicle type. 
        if (allVehicles.Contains("roCKeTs"))
        {
            Console.WriteLine("\nThe 'All' vehicles set contains 'roCKeTs'");
        }

        allVehicles.ExceptWith(someVehicles);
        Console.WriteLine("\nThe excepted HashSet contains:\n");
        foreach (string vehicle in allVehicles)
        {
            Console.WriteLine(vehicle);
        }

        // Remove all the vehicles that are not 'super cool'.
        allVehicles.RemoveWhere(isNotSuperCool);

        Console.WriteLine("\nThe super cool vehicles are:\n");
        foreach (string vehicle in allVehicles)
        {
            Console.WriteLine(vehicle);
        }
    }

    // Predicate to determine vehicle 'coolness'. 
    private static bool isNotSuperCool(string vehicle)
    {
        bool superCool = (vehicle == "Helicopters") || (vehicle == "Motorcycles");

        return !superCool;
    }
}

class SameVehicleComparer : EqualityComparer<string>
{
    public override bool Equals(string s1, string s2)
    {
        return s1.Equals(s2, StringComparison.CurrentCultureIgnoreCase);
    }


    public override int GetHashCode(string s)
    {
        return base.GetHashCode();
    }
}

// The program writes the following output to the console. 
// 
// The current HashSet contains: 
// 
// Planes 
// Trains 
// Automobiles 
// 
// The updated HashSet contains: 
// 
// Planes 
// Trains 
// Automobiles 
// Ships 
// Motorcycles 
// Rockets 
// Helicopters 
// Submarines 
// 
// The 'All' vehicles set contains everything in 'Some' vechicles list. 
// 
// The 'All' vehicles set contains 'roCKeTs' 
// 
// The excepted HashSet contains: 
// 
// Ships 
// Motorcycles 
// Rockets 
// Helicopters 
// Submarines 
// 
// The super cool vehicles are: 
// 
// Motorcycles 
// Helicopters

.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

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft