Skip to content

API reference Obsidian.API.Utilities.Concurrency

ConcurrentHashSet<T> class

Represents a thread-safe hash-based unique collection.

public class ConcurrentHashSet<T> : IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable, ICollection<T>

In Obsidian.API.

Type parameters

T
The type of the items in the collection.

Remarks

All public members of ConcurrentHashSet are thread-safe and may be used concurrently from multiple threads.

Constructors

ConcurrentHashSet()

public ConcurrentHashSet();

Initializes a new instance of the ConcurrentHashSet class that is empty, has the default concurrency level, has the default initial capacity, and uses the default comparer for the item type.

ConcurrentHashSet(IEnumerable<T>)

public ConcurrentHashSet(IEnumerable<T> collection);

Initializes a new instance of the ConcurrentHashSet class that contains elements copied from the specified IEnumerable{T}, has the default concurrency level, has the default initial capacity, and uses the default comparer for the item type.

Parameters

collection
The IEnumerable{T} whose elements are copied to the new ConcurrentHashSet.

ConcurrentHashSet(IEnumerable<T>, IEqualityComparer<T>)

public ConcurrentHashSet(IEnumerable<T> collection, IEqualityComparer<T> comparer);

Initializes a new instance of the ConcurrentHashSet class that contains elements copied from the specified IEnumerable, has the default concurrency level, has the default initial capacity, and uses the specified IEqualityComparer{T}.

Parameters

collection
The IEnumerable{T} whose elements are copied to the new ConcurrentHashSet.
comparer
The IEqualityComparer{T} implementation to use when comparing items.

ConcurrentHashSet(IEqualityComparer<T>)

public ConcurrentHashSet(IEqualityComparer<T> comparer);

Initializes a new instance of the ConcurrentHashSet class that is empty, has the specified concurrency level and capacity, and uses the specified IEqualityComparer{T}.

Parameters

comparer
The IEqualityComparer{T} implementation to use when comparing items.

ConcurrentHashSet(int, IEnumerable<T>, IEqualityComparer<T>)

public ConcurrentHashSet(int concurrencyLevel, IEnumerable<T> collection, IEqualityComparer<T> comparer);

Initializes a new instance of the ConcurrentHashSet class that contains elements copied from the specified IEnumerable, has the specified concurrency level, has the specified initial capacity, and uses the specified IEqualityComparer{T}.

Parameters

concurrencyLevel
The estimated number of threads that will update the ConcurrentHashSet concurrently.
collection
The IEnumerable{T} whose elements are copied to the new ConcurrentHashSet.
comparer
The IEqualityComparer{T} implementation to use when comparing items.

ConcurrentHashSet(int, int)

public ConcurrentHashSet(int concurrencyLevel, int capacity);

Initializes a new instance of the ConcurrentHashSet class that is empty, has the specified concurrency level and capacity, and uses the default comparer for the item type.

Parameters

concurrencyLevel
The estimated number of threads that will update the ConcurrentHashSet concurrently.
capacity
The initial number of elements that the ConcurrentHashSet can contain.

ConcurrentHashSet(int, int, IEqualityComparer<T>)

public ConcurrentHashSet(int concurrencyLevel, int capacity, IEqualityComparer<T> comparer);

Initializes a new instance of the ConcurrentHashSet class that is empty, has the specified concurrency level, has the specified initial capacity, and uses the specified IEqualityComparer{T}.

Parameters

concurrencyLevel
The estimated number of threads that will update the ConcurrentHashSet concurrently.
capacity
The initial number of elements that the ConcurrentHashSet can contain.
comparer
The IEqualityComparer{T} implementation to use when comparing items.

Properties

Count

public int Count { get; }

Gets the number of items contained in the ConcurrentHashSet.

Value

The number of items contained in the ConcurrentHashSet.

Remarks

Count has snapshot semantics and represents the number of items in the ConcurrentHashSet at the moment when Count was accessed.

IsEmpty

public bool IsEmpty { get; }

Gets a value that indicates whether the ConcurrentHashSet is empty.

Value

true if the ConcurrentHashSet is empty; otherwise, false.

Methods

Add(T)

public bool Add(T item);

Adds the specified item to the ConcurrentHashSet.

Parameters

item
The item to add.

Returns

true if the items was added to the ConcurrentHashSet successfully; false if it already exists.

Clear()

public void Clear();

Removes all items from the ConcurrentHashSet.

Contains(T)

public bool Contains(T item);

Determines whether the ConcurrentHashSet contains the specified item.

Parameters

item
The item to locate in the ConcurrentHashSet.

Returns

true if the ConcurrentHashSet contains the item; otherwise, false.

GetEnumerator()

public IEnumerator<T> GetEnumerator();

Returns an enumerator that iterates through the ConcurrentHashSet.

Returns

An enumerator for the ConcurrentHashSet.

Remarks

The enumerator returned from the collection is safe to use concurrently with reads and writes to the collection, however it does not represent a moment-in-time snapshot of the collection. The contents exposed through the enumerator may contain modifications made to the collection after ConcurrentHashSet.GetEnumerator was called.

ToArray()

public T[] ToArray();

Copies the items to a new array, as of one moment.

Remarks

Unlike Enumerable.ToArray, which counts the items before copying them, it doesn't fail when another thread adds items meanwhile: it counts and copies under the same locks.

TryRemove(T)

public bool TryRemove(T item);

Attempts to remove the item from the ConcurrentHashSet.

Parameters

item
The item to remove.

Returns

true if an item was removed successfully; otherwise, false.

On this page