Skip to main content

HashSet<T> Class

NuGet: GHIElectronics.TinyCLR.Collections
Assembly: GHIElectronics.TinyCLR.Collections
Namespace: System.Collections.Generic

Hash-table-based generic set. Mirrors the .NET BCL surface for the subset we ship. Same chained-collision layout and prime-table sizing as Dictionary{TKey, TValue}; the entry holds the element directly instead of a key/value pair. null IS a valid element when T is a reference type — the comparer (EqualityComparer{T}.Default) treats null consistently (GetHashCode(null) = 0, Equals(null, null) = true).

public class HashSet<T> : ISet<T>, ICollection<T>, IEnumerable<T>, ICollection, IEnumerable

Constructors

HashSet()

public HashSet() : this(0, null)

Initializes a new empty set that uses the default equality comparer.

HashSet(IEqualityComparer<T> comparer)

public HashSet(IEqualityComparer<T> comparer) : this(0, comparer)

Initializes a new empty set that uses the specified equality comparer.

HashSet(int capacity)

public HashSet(int capacity) : this(capacity, null)

Initializes a new empty set with the specified initial capacity.

HashSet(int capacity, IEqualityComparer<T> comparer)

public HashSet(int capacity, IEqualityComparer<T> comparer)

Initializes a new empty set with the specified initial capacity and equality comparer.

HashSet(IEnumerable<T> collection)

public HashSet(IEnumerable<T> collection) : this(collection, null)

Initializes a new set that contains the elements copied from the specified collection.

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

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

Initializes a new set that contains the elements copied from the specified collection and uses the specified equality comparer.

Properties

Comparer

public IEqualityComparer<T> Comparer { get; }

Gets the equality comparer used to determine equality of elements in the set.

Count

public int Count { get; }

Gets the number of elements contained in the set.

Methods

Add(T item)

public bool Add(T item)

Adds the specified element to the set and returns whether it was newly added.

Returns bool

Clear()

public void Clear()

Removes all elements from the set.

Contains(T item)

public bool Contains(T item)

Determines whether the set contains the specified element.

Returns bool

Remove(T item)

public bool Remove(T item)

Removes the specified element from the set and returns whether it was found and removed.

Returns bool

RemoveWhere(Predicate<T> match)

public int RemoveWhere(Predicate<T> match)

Removes all elements that match the conditions defined by the specified predicate and returns the number removed.

Returns int

TryGetValue(T equalValue, out T actualValue)

public bool TryGetValue(T equalValue, out T actualValue)

Searches the set for the element that equals the specified value and returns whether it was found.

Returns bool

CopyTo(T[] array)

public void CopyTo(T[] array)

Copies the elements of the set to the specified array.

CopyTo(T[] array, int arrayIndex)

public void CopyTo(T[] array, int arrayIndex)

Copies the elements of the set to the specified array, starting at the specified array index.

CopyTo(T[] array, int arrayIndex, int count)

public void CopyTo(T[] array, int arrayIndex, int count)

Copies the specified number of elements of the set to the specified array, starting at the specified array index.

UnionWith(IEnumerable<T> other)

public void UnionWith(IEnumerable<T> other)

Modifies the set to contain all elements that are present in itself, the specified collection, or both.

IntersectWith(IEnumerable<T> other)

public void IntersectWith(IEnumerable<T> other)

Modifies the set to contain only elements that are also present in the specified collection.

ExceptWith(IEnumerable<T> other)

public void ExceptWith(IEnumerable<T> other)

Removes all elements in the specified collection from the set.

SymmetricExceptWith(IEnumerable<T> other)

public void SymmetricExceptWith(IEnumerable<T> other)

Modifies the set to contain only elements that are present either in itself or in the specified collection, but not both.

IsSubsetOf(IEnumerable<T> other)

public bool IsSubsetOf(IEnumerable<T> other)

Determines whether the set is a subset of the specified collection.

Returns bool

IsProperSubsetOf(IEnumerable<T> other)

public bool IsProperSubsetOf(IEnumerable<T> other)

Determines whether the set is a proper subset of the specified collection.

Returns bool

IsSupersetOf(IEnumerable<T> other)

public bool IsSupersetOf(IEnumerable<T> other)

Determines whether the set is a superset of the specified collection.

Returns bool

IsProperSupersetOf(IEnumerable<T> other)

public bool IsProperSupersetOf(IEnumerable<T> other)

Determines whether the set is a proper superset of the specified collection.

Returns bool

Overlaps(IEnumerable<T> other)

public bool Overlaps(IEnumerable<T> other)

Determines whether the set and the specified collection share any common elements.

Returns bool

SetEquals(IEnumerable<T> other)

public bool SetEquals(IEnumerable<T> other)

Determines whether the set and the specified collection contain the same elements.

Returns bool

GetEnumerator()

public Enumerator GetEnumerator()

Returns an enumerator that iterates through the set.

Returns Enumerator