Class ThreadSafeSet<T>
A thread-safe implementation of a set data structure. Uses a reader-writer lock to synchronize access to the underlying HashSet.
public class ThreadSafeSet<T> : IDisposable
Type Parameters
T
The type of elements in the set.
- Inheritance
-
ThreadSafeSet<T>
- Implements
- Inherited Members
- Extension Methods
Constructors
ThreadSafeSet(HashSet<T>, LockRecursionPolicy)
Initializes a new instance of the ThreadSafeSet<T> class with the specified HashSet and lock recursion policy.
public ThreadSafeSet(HashSet<T> src, LockRecursionPolicy lockRecursionPolicy)
Parameters
src
HashSet<T>The source HashSet to use.
lockRecursionPolicy
LockRecursionPolicyThe lock recursion policy to use.
ThreadSafeSet(int, LockRecursionPolicy)
Initializes a new instance of the ThreadSafeSet<T> class with the specified capacity and lock recursion policy.
public ThreadSafeSet(int capacity, LockRecursionPolicy lockRecursionPolicy)
Parameters
capacity
intThe initial capacity of the set.
lockRecursionPolicy
LockRecursionPolicyThe lock recursion policy to use.
ThreadSafeSet(LockRecursionPolicy)
Initializes a new instance of the ThreadSafeSet<T> class with the specified lock recursion policy.
public ThreadSafeSet(LockRecursionPolicy lockRecursionPolicy)
Parameters
lockRecursionPolicy
LockRecursionPolicyThe lock recursion policy to use.
Properties
Content
Gets the underlying HashSet that stores the elements.
public HashSet<T> Content { get; }
Property Value
- HashSet<T>
Count
Gets the number of elements in the set.
public int Count { get; }
Property Value
ReaderWriterLock
Gets the reader-writer lock used to synchronize access to the set.
public ReaderWriterLockSlim ReaderWriterLock { get; }
Property Value
Methods
Add(T)
Adds an element to the set.
public bool Add(T item)
Parameters
item
TThe element to add.
Returns
- bool
true if the element is added to the set; false if the element is already present.
Clear()
Removes all elements from the set.
public void Clear()
CloneContent()
Creates a new HashSet that contains the same elements as the current set.
public HashSet<T> CloneContent()
Returns
- HashSet<T>
A new HashSet that contains the same elements as the current set.
Contains(T)
Determines whether the set contains the specified element.
public bool Contains(T item)
Parameters
item
TThe element to locate.
Returns
- bool
true if the set contains the specified element; otherwise, false.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Dispose(bool)
Releases the unmanaged resources used by the ThreadSafeSet<T> and optionally releases the managed resources.
protected virtual void Dispose(bool disposing)
Parameters
disposing
booltrue to release both managed and unmanaged resources; false to release only unmanaged resources.
EnsureCapacity(int)
Ensures that the set can hold the specified number of elements without growing.
public int EnsureCapacity(int capacity)
Parameters
capacity
intThe minimum capacity to ensure.
Returns
- int
The new capacity of the set.
Remove(T)
Removes the specified element from the set.
public bool Remove(T item)
Parameters
item
TThe element to remove.
Returns
- bool
true if the element is successfully found and removed; otherwise, false.
TrimExcess()
Sets the capacity of the set to the actual number of elements it contains.
public void TrimExcess()
TryGetValue(T, out T)
Attempts to get the actual value of an element equal to the specified value.
public bool TryGetValue(T equalValue, out T actualValue)
Parameters
equalValue
TThe value to search for.
actualValue
TWhen this method returns, contains the actual value if found; otherwise, the default value for the type.
Returns
- bool
true if the set contains an element equal to the specified value; otherwise, false.
UnionWith(IEnumerable<T>)
Modifies the current set 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 set.