Microsoft Docs

🌙

System.Collections.Generic.HashSet<T> Class

Represents a set of values. Provides high-performance set operations.

Constructors

HashSet()â–¶

Initializes a new empty HashSet that uses the default equality comparer for the type of the elements.

public HashSet();
HashSet(IEqualityComparer<T> comparer)â–¶

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

public HashSet(IEqualityComparer<T> comparer);
HashSet(IEnumerable<T> collection)â–¶

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

public HashSet(IEnumerable<T> collection);

Properties

int Count { get; }â–¶

Gets the number of elements contained in the HashSet.

public int Count { get; }
bool IsReadOnly { get; }â–¶

Gets a value indicating whether the HashSet is read-only.

public bool IsReadOnly { get; }

Methods

bool Add(T item)â–¶

Adds the specified element to the set.

public bool Add(T item);

Returns true if the element is added; false if the element already exists.

bool Contains(T item)â–¶

Determines whether the set contains the specified element.

public bool Contains(T item);
bool Remove(T item)â–¶

Removes the specified element from the set.

public bool Remove(T item);
void UnionWith(IEnumerable<T> other)â–¶

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

public void UnionWith(IEnumerable<T> other);
void IntersectWith(IEnumerable<T> other)â–¶

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

public void IntersectWith(IEnumerable<T> other);
void ExceptWith(IEnumerable<T> other)â–¶

Removes all elements in the specified collection from the current set.

public void ExceptWith(IEnumerable<T> other);
void SymmetricExceptWith(IEnumerable<T> other)â–¶

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

public void SymmetricExceptWith(IEnumerable<T> other);
void CopyTo(T[] array, int arrayIndex)â–¶

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

public void CopyTo(T[] array, int arrayIndex);
IEnumerator<T> GetEnumerator()â–¶

Returns an enumerator that iterates through the HashSet.

public IEnumerator<T> GetEnumerator();

Explicit Interface Implementations

void ICollection<T>.Add(T item)â–¶
void ICollection<T>.Add(T item);