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 : notnullType 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);Nested types
| Name | Description |
|---|---|
| ConcurrentHashSet<T>.KeyEnumerator | Enumerates the elements of a ConcurrentHashSet<T>. |
Constructors
| Name | Description |
|---|---|
| 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.csInitializes a new instance of the ConcurrentHashSet<T> class.
public ConcurrentHashSet()ConcurrentHashSet(IEqualityComparer<T>)
ConcurrentHashSet.csInitializes a new instance of the ConcurrentHashSet<T> class with the specified equality comparer.
public ConcurrentHashSet(IEqualityComparer<T> equalityComparer)Parameters
equalityComparerIEqualityComparer<T>- The equality comparer to use when comparing values in the set.
Properties
| Name | Description |
|---|---|
| Count | Gets the number of elements contained in the ConcurrentHashSet<T>. |
| IsEmpty | Gets a value that indicates whether the ConcurrentHashSet<T> is empty. |
| IsReadOnly | Gets a value indicating whether the ConcurrentHashSet<T> is read-only. |
Count
ConcurrentHashSet.csGets the number of elements contained in the ConcurrentHashSet<T>.
public int Count { get; }Property value
IsEmpty
ConcurrentHashSet.csGets a value that indicates whether the ConcurrentHashSet<T> is empty.
public bool IsEmpty { get; }Property value
IsReadOnly
ConcurrentHashSet.csGets a value indicating whether the ConcurrentHashSet<T> is read-only.
public bool IsReadOnly { get; }Property value
Methods
| Name | Description |
|---|---|
| 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. |
Add(T)
ConcurrentHashSet.csAttempts to add the specified element to the ConcurrentHashSet<T>.
public bool Add(T value)Parameters
valueT- 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.csAdds the specified values to the ConcurrentHashSet<T>.
public void AddRange(IEnumerable<T>? values)Parameters
valuesIEnumerable<T>?- The values to add.
AddRange(ReadOnlySpan<T>)
ConcurrentHashSet.csAdds the specified values to the ConcurrentHashSet<T>.
public void AddRange(params scoped ReadOnlySpan<T> values)Parameters
valuesReadOnlySpan<T>- The values to add.
Clear()
ConcurrentHashSet.csRemoves all elements from the ConcurrentHashSet<T>.
public void Clear()Contains(T)
ConcurrentHashSet.csDetermines whether the ConcurrentHashSet<T> contains the specified element.
public bool Contains(T item)Parameters
itemT- The element to locate in the
ConcurrentHashSet<T>.
Returns
bool true if the ConcurrentHashSet<T> contains the element; otherwise, false.
CopyTo(T[], int)
ConcurrentHashSet.csCopies the elements of the ConcurrentHashSet<T> to an array, starting at the specified array index.
Parameters
arrayT[]- The destination array.
arrayIndexint- The zero-based index in the array at which copying begins.
Exceptions
ArgumentNullExceptionarrayisnull.ArgumentOutOfRangeExceptionarrayIndexis negative.ArgumentException- The number of elements in the set is greater than the available space from
arrayIndexto the end ofarray.
ExceptWith(IEnumerable<T>)
ConcurrentHashSet.csRemoves all elements in the specified collection from the current ConcurrentHashSet<T>.
public void ExceptWith(IEnumerable<T> other)Parameters
otherIEnumerable<T>- The collection to remove from the current
ConcurrentHashSet<T>.
GetEnumerator()
ConcurrentHashSet.csReturns an enumerator that iterates through the ConcurrentHashSet<T>.
public ConcurrentHashSet<T>.KeyEnumerator GetEnumerator()Returns
ConcurrentHashSet<T>.KeyEnumerator An enumerator for the ConcurrentHashSet<T>.
IntersectWith(IEnumerable<T>)
ConcurrentHashSet.csModifies 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
otherIEnumerable<T>- The collection to compare to the current
ConcurrentHashSet<T>.
IsProperSubsetOf(IEnumerable<T>)
ConcurrentHashSet.csDetermines whether the current ConcurrentHashSet<T> is a proper subset of a specified collection.
public bool IsProperSubsetOf(IEnumerable<T> other)Parameters
otherIEnumerable<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.csDetermines whether the current ConcurrentHashSet<T> is a proper superset of a specified collection.
public bool IsProperSupersetOf(IEnumerable<T> other)Parameters
otherIEnumerable<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.csDetermines whether the current ConcurrentHashSet<T> is a subset of a specified collection.
public bool IsSubsetOf(IEnumerable<T> other)Parameters
otherIEnumerable<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.csDetermines whether the current ConcurrentHashSet<T> is a superset of a specified collection.
public bool IsSupersetOf(IEnumerable<T> other)Parameters
otherIEnumerable<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.csDetermines whether the current ConcurrentHashSet<T> overlaps with the specified collection.
public bool Overlaps(IEnumerable<T> other)Parameters
otherIEnumerable<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.
Remove(T)
ConcurrentHashSet.csAttempts to remove the specified element from the ConcurrentHashSet<T>.
public bool Remove(T item)Parameters
itemT- The element to remove.
Returns
bool true if the element was removed successfully; otherwise, false.
SetEquals(IEnumerable<T>)
ConcurrentHashSet.csDetermines whether the current ConcurrentHashSet<T> and the specified collection contain the same elements.
public bool SetEquals(IEnumerable<T> other)Parameters
otherIEnumerable<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.csModifies 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
otherIEnumerable<T>- The collection to compare to the current
ConcurrentHashSet<T>.
System.Collections.Generic.ICollection<T>.Add(T)
ConcurrentHashSet.csSystem.Collections.Generic.IEnumerable<T>.GetEnumerator()
ConcurrentHashSet.csSystem.Collections.IEnumerable.GetEnumerator()
ConcurrentHashSet.csUnionWith(IEnumerable<T>)
ConcurrentHashSet.csModifies 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
otherIEnumerable<T>- The collection to compare to the current
ConcurrentHashSet<T>.