ConcurrentHashSet<T> Class

Represents a thread-safe set of unique values.

public sealed class ConcurrentHashSet<T> : ICollection<T>, IEnumerable<T>, IReadOnlyCollection<T>, IReadOnlySet<T>, ISet<T>, IEnumerable where T : notnull

Type parameters

T
The type of elements in the set.

Remarks

The set comparison methods (ConcurrentHashSet<T>.IsSubsetOf, ConcurrentHashSet<T>.IsSupersetOf, ConcurrentHashSet<T>.Overlaps, ConcurrentHashSet<T>.SetEquals and their proper variants) do not take an atomic snapshot of the set: elements can be added or removed while they run, so their result is best-effort and only reflects a set that was not modified concurrently.

Example

var set = new ConcurrentHashSet<int>();
set.Add(1);
set.Add(2);
Assert.Contains(1, set);
Implements

Nested types

NameDescription
ConcurrentHashSet<T>.KeyEnumeratorEnumerates the elements of a ConcurrentHashSet<T>.

Constructors

NameDescription
ConcurrentHashSet()Initializes a new instance of the ConcurrentHashSet<T> class.
ConcurrentHashSet(IEqualityComparer<T>)Initializes a new instance of the ConcurrentHashSet<T> class with the specified equality comparer.

ConcurrentHashSet overloads

ConcurrentHashSet()

ConcurrentHashSet.cs

Initializes a new instance of the ConcurrentHashSet<T> class.

public ConcurrentHashSet()

ConcurrentHashSet(IEqualityComparer<T>)

ConcurrentHashSet.cs

Initializes a new instance of the ConcurrentHashSet<T> class with the specified equality comparer.

public ConcurrentHashSet(IEqualityComparer<T> equalityComparer)
Parameters
equalityComparer IEqualityComparer<T>
The equality comparer to use when comparing values in the set.

Properties

NameDescription
CountGets the number of elements contained in the ConcurrentHashSet<T>.
IsEmptyGets a value that indicates whether the ConcurrentHashSet<T> is empty.
IsReadOnlyGets a value indicating whether the ConcurrentHashSet<T> is read-only.

Gets the number of elements contained in the ConcurrentHashSet<T>.

public int Count { get; }

Property value

int

Gets a value that indicates whether the ConcurrentHashSet<T> is empty.

public bool IsEmpty { get; }

Property value

bool

Gets a value indicating whether the ConcurrentHashSet<T> is read-only.

public bool IsReadOnly { get; }

Property value

bool

Methods

NameDescription
Add(T)Attempts to add the specified element to the ConcurrentHashSet<T>.
AddRange(IEnumerable<T>?)Adds the specified values to the ConcurrentHashSet<T>.
AddRange(ReadOnlySpan<T>)Adds the specified values to the ConcurrentHashSet<T>.
Clear()Removes all elements from the ConcurrentHashSet<T>.
Contains(T)Determines whether the ConcurrentHashSet<T> contains the specified element.
CopyTo(T[], int)Copies the elements of the ConcurrentHashSet<T> to an array, starting at the specified array index.
ExceptWith(IEnumerable<T>)Removes all elements in the specified collection from the current ConcurrentHashSet<T>.
GetEnumerator()Returns an enumerator that iterates through the ConcurrentHashSet<T>.
IntersectWith(IEnumerable<T>)Modifies the current ConcurrentHashSet<T> to contain only elements that are present in itself and in the specified collection.
IsProperSubsetOf(IEnumerable<T>)Determines whether the current ConcurrentHashSet<T> is a proper subset of a specified collection.
IsProperSupersetOf(IEnumerable<T>)Determines whether the current ConcurrentHashSet<T> is a proper superset of a specified collection.
IsSubsetOf(IEnumerable<T>)Determines whether the current ConcurrentHashSet<T> is a subset of a specified collection.
IsSupersetOf(IEnumerable<T>)Determines whether the current ConcurrentHashSet<T> is a superset of a specified collection.
Overlaps(IEnumerable<T>)Determines whether the current ConcurrentHashSet<T> overlaps with the specified collection.
Remove(T)Attempts to remove the specified element from the ConcurrentHashSet<T>.
SetEquals(IEnumerable<T>)Determines whether the current ConcurrentHashSet<T> and the specified collection contain the same elements.
SymmetricExceptWith(IEnumerable<T>)Modifies the current ConcurrentHashSet<T> to contain only elements that are present either in itself or in the specified collection, but not both.
System.Collections.Generic.ICollection<T>.Add(T)
System.Collections.Generic.IEnumerable<T>.GetEnumerator()
System.Collections.IEnumerable.GetEnumerator()
UnionWith(IEnumerable<T>)Modifies the current ConcurrentHashSet<T> to contain all elements that are present in itself, the specified collection, or both.

Attempts to add the specified element to the ConcurrentHashSet<T>.

public bool Add(T value)

Parameters

value T
The element to add to the set.

Returns

bool true if the element was added to the ConcurrentHashSet<T> successfully; false if the element already exists.

AddRange overloads

AddRange(IEnumerable<T>?)

ConcurrentHashSet.cs

Adds the specified values to the ConcurrentHashSet<T>.

public void AddRange(IEnumerable<T>? values)
Parameters
values IEnumerable<T>?
The values to add.

AddRange(ReadOnlySpan<T>)

ConcurrentHashSet.cs

Adds the specified values to the ConcurrentHashSet<T>.

public void AddRange(params scoped ReadOnlySpan<T> values)
Parameters
values ReadOnlySpan<T>
The values to add.

Removes all elements from the ConcurrentHashSet<T>.

public void Clear()

Determines whether the ConcurrentHashSet<T> contains the specified element.

public bool Contains(T item)

Parameters

item T
The element to locate in the ConcurrentHashSet<T>.

Returns

bool true if the ConcurrentHashSet<T> contains the element; otherwise, false.

CopyTo(T[], int)

ConcurrentHashSet.cs

Copies the elements of the ConcurrentHashSet<T> to an array, starting at the specified array index.

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

Parameters

array T[]
The destination array.
arrayIndex int
The zero-based index in the array at which copying begins.

Exceptions

ArgumentNullException
array is null.
ArgumentOutOfRangeException
arrayIndex is negative.
ArgumentException
The number of elements in the set is greater than the available space from arrayIndex to the end of array.

ExceptWith(IEnumerable<T>)

ConcurrentHashSet.cs

Removes all elements in the specified collection from the current ConcurrentHashSet<T>.

public void ExceptWith(IEnumerable<T> other)

Parameters

other IEnumerable<T>
The collection to remove from the current ConcurrentHashSet<T>.

GetEnumerator()

ConcurrentHashSet.cs

Returns an enumerator that iterates through the ConcurrentHashSet<T>.

public ConcurrentHashSet<T>.KeyEnumerator GetEnumerator()

IntersectWith(IEnumerable<T>)

ConcurrentHashSet.cs

Modifies the current ConcurrentHashSet<T> to contain only elements that are present in itself and in the specified collection.

public void IntersectWith(IEnumerable<T> other)

Parameters

other IEnumerable<T>
The collection to compare to the current ConcurrentHashSet<T>.

IsProperSubsetOf(IEnumerable<T>)

ConcurrentHashSet.cs

Determines whether the current ConcurrentHashSet<T> is a proper subset of a specified collection.

public bool IsProperSubsetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>
The collection to compare to the current ConcurrentHashSet<T>.

Returns

bool true if the current ConcurrentHashSet<T> is a proper subset of other; otherwise, false.

IsProperSupersetOf(IEnumerable<T>)

ConcurrentHashSet.cs

Determines whether the current ConcurrentHashSet<T> is a proper superset of a specified collection.

public bool IsProperSupersetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>
The collection to compare to the current ConcurrentHashSet<T>.

Returns

bool true if the current ConcurrentHashSet<T> is a proper superset of other; otherwise, false.

Remarks

other is enumerated only until an element that is not in the set is found, so the elements after it are never looked up. The set itself is never copied: only the distinct elements of other are tracked.

IsSubsetOf(IEnumerable<T>)

ConcurrentHashSet.cs

Determines whether the current ConcurrentHashSet<T> is a subset of a specified collection.

public bool IsSubsetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>
The collection to compare to the current ConcurrentHashSet<T>.

Returns

bool true if the current ConcurrentHashSet<T> is a subset of other; otherwise, false.

IsSupersetOf(IEnumerable<T>)

ConcurrentHashSet.cs

Determines whether the current ConcurrentHashSet<T> is a superset of a specified collection.

public bool IsSupersetOf(IEnumerable<T> other)

Parameters

other IEnumerable<T>
The collection to compare to the current ConcurrentHashSet<T>.

Returns

bool true if the current ConcurrentHashSet<T> is a superset of other; otherwise, false.

Remarks

other is enumerated only until an element that is not in the set is found, so the elements after it are never looked up.

Overlaps(IEnumerable<T>)

ConcurrentHashSet.cs

Determines whether the current ConcurrentHashSet<T> overlaps with the specified collection.

public bool Overlaps(IEnumerable<T> other)

Parameters

other IEnumerable<T>
The collection to compare to the current ConcurrentHashSet<T>.

Returns

bool true if the current ConcurrentHashSet<T> and other share at least one common element; otherwise, false.

Remarks

other is enumerated only until a common element is found, so the elements after it are never looked up. It is not enumerated at all when the set is empty.

Attempts to remove the specified element from the ConcurrentHashSet<T>.

public bool Remove(T item)

Parameters

item T
The element to remove.

Returns

bool true if the element was removed successfully; otherwise, false.

SetEquals(IEnumerable<T>)

ConcurrentHashSet.cs

Determines whether the current ConcurrentHashSet<T> and the specified collection contain the same elements.

public bool SetEquals(IEnumerable<T> other)

Parameters

other IEnumerable<T>
The collection to compare to the current ConcurrentHashSet<T>.

Returns

bool true if the current ConcurrentHashSet<T> is equal to other; otherwise, false.

SymmetricExceptWith(IEnumerable<T>)

ConcurrentHashSet.cs

Modifies the current ConcurrentHashSet<T> to contain only elements that are present either in itself or in the specified collection, but not both.

public void SymmetricExceptWith(IEnumerable<T> other)

Parameters

other IEnumerable<T>
The collection to compare to the current ConcurrentHashSet<T>.

System.Collections.Generic.ICollection<T>.Add(T)

ConcurrentHashSet.cs
void ICollection<T>.Add(T item)

Parameters

item T

No documentation is available.

System.Collections.Generic.IEnumerable<T>.GetEnumerator()

ConcurrentHashSet.cs
IEnumerator<T> IEnumerable<T>.GetEnumerator()

No documentation is available.

System.Collections.IEnumerable.GetEnumerator()

ConcurrentHashSet.cs
IEnumerator IEnumerable.GetEnumerator()

No documentation is available.

UnionWith(IEnumerable<T>)

ConcurrentHashSet.cs

Modifies the current ConcurrentHashSet<T> to contain all elements that are present in itself, the specified collection, or both.

public void UnionWith(IEnumerable<T> other)

Parameters

other IEnumerable<T>
The collection to compare to the current ConcurrentHashSet<T>.