Class SyncDictionary<TKey, TValue>
- Namespace
- Utils.Common.Collections.Synchronized
- Assembly
- Utils.Common.dll
A dictionary whose individual operations lock on SyncRoot, so single calls are safe from several threads. Compound operations are not atomic unless the caller locks on SyncRoot; GetOrAdd(TKey, Func<TKey, TValue>), TryAdd(TKey, TValue) and TryGetAndRemove(TKey) cover the common cases. Cache is an array snapshot of the values that is rebuilt lazily after every modification.
public class SyncDictionary<TKey, TValue> where TKey : notnull
Type Parameters
TKeyThe type of the keys.
TValueThe type of the values.
- Inheritance
-
objectSyncDictionary<TKey, TValue>
- Extension Methods
Constructors
SyncDictionary()
Creates an empty dictionary with the default key comparer.
public SyncDictionary()
SyncDictionary(IEnumerable<TValue>, Func<TValue, TKey>)
Creates a dictionary filled with the given values, each stored under the key computed by getKey.
public SyncDictionary(IEnumerable<TValue> values, Func<TValue, TKey> getKey)
Parameters
valuesIEnumerable<TValue>The values to add.
getKeyFunc<TValue, TKey>Returns the key of a value.
Exceptions
- ArgumentException
Two values have the same key.
SyncDictionary(IEqualityComparer<TKey>)
Creates an empty dictionary with the given key comparer.
public SyncDictionary(IEqualityComparer<TKey> keyComparer)
Parameters
keyComparerIEqualityComparer<TKey>The comparer used to compare keys.
Exceptions
- ArgumentNullException
keyCompareris null.
SyncDictionary(int)
Creates an empty dictionary with the given initial capacity and the default key comparer.
public SyncDictionary(int capacity)
Parameters
capacityintThe initial capacity.
SyncDictionary(int, IEqualityComparer<TKey>)
Creates an empty dictionary with the given initial capacity and key comparer.
public SyncDictionary(int capacity, IEqualityComparer<TKey> keyComparer)
Parameters
capacityintThe initial capacity.
keyComparerIEqualityComparer<TKey>The comparer used to compare keys.
Exceptions
- ArgumentNullException
keyCompareris null.
Properties
Cache
A snapshot array of the values, safe to enumerate while other threads modify the dictionary. The same array is returned until the next modification; do not change it.
public TValue[] Cache { get; }
Property Value
- TValue[]
Count
Gets the number of elements contained in the System.Collections.Generic.ICollection<T>.
public int Count { get; }
Property Value
- int
The number of elements contained in the System.Collections.Generic.ICollection<T>.
IsReadOnly
Gets a value indicating whether the System.Collections.Generic.ICollection<T> is read-only.
public bool IsReadOnly { get; }
Property Value
- bool
true if the System.Collections.Generic.ICollection<T> is read-only; otherwise, false.
this[TKey]
Gets or sets the element with the specified key.
public TValue this[TKey key] { get; set; }
Parameters
keyTKeyThe key of the element to get or set.
Property Value
- TValue
The element with the specified key.
Exceptions
- ArgumentNullException
keyis null.- KeyNotFoundException
The property is retrieved and
keyis not found.- NotSupportedException
The property is set and the System.Collections.Generic.IDictionary<TKey, TValue> is read-only.
Keys
Gets an System.Collections.Generic.ICollection<T> containing the keys of the System.Collections.Generic.IDictionary<TKey, TValue>.
public ICollection<TKey> Keys { get; }
Property Value
- ICollection<TKey>
An System.Collections.Generic.ICollection<T> containing the keys of the object that implements System.Collections.Generic.IDictionary<TKey, TValue>.
SyncRoot
The object every operation of this dictionary locks on. Lock on it to make several calls atomic.
public object SyncRoot { get; }
Property Value
- object
Values
Gets an System.Collections.Generic.ICollection<T> containing the values in the System.Collections.Generic.IDictionary<TKey, TValue>.
public ICollection<TValue> Values { get; }
Property Value
- ICollection<TValue>
An System.Collections.Generic.ICollection<T> containing the values in the object that implements System.Collections.Generic.IDictionary<TKey, TValue>.
Methods
Add(KeyValuePair<TKey, TValue>)
Adds an item to the System.Collections.Generic.ICollection<T>.
public void Add(KeyValuePair<TKey, TValue> item)
Parameters
itemKeyValuePair<TKey, TValue>The object to add to the System.Collections.Generic.ICollection<T>.
Exceptions
- NotSupportedException
The System.Collections.Generic.ICollection<T> is read-only.
Add(TKey, TValue)
Adds an element with the provided key and value to the System.Collections.Generic.IDictionary<TKey, TValue>.
public void Add(TKey key, TValue value)
Parameters
keyTKeyThe object to use as the key of the element to add.
valueTValueThe object to use as the value of the element to add.
Exceptions
- ArgumentNullException
keyis null.- ArgumentException
An element with the same key already exists in the System.Collections.Generic.IDictionary<TKey, TValue>.
- NotSupportedException
The System.Collections.Generic.IDictionary<TKey, TValue> is read-only.
Clear()
Removes all items from the System.Collections.Generic.ICollection<T>.
public void Clear()
Exceptions
- NotSupportedException
The System.Collections.Generic.ICollection<T> is read-only.
Contains(KeyValuePair<TKey, TValue>)
Determines whether the System.Collections.Generic.ICollection<T> contains a specific value.
public bool Contains(KeyValuePair<TKey, TValue> item)
Parameters
itemKeyValuePair<TKey, TValue>The object to locate in the System.Collections.Generic.ICollection<T>.
Returns
- bool
true if
itemis found in the System.Collections.Generic.ICollection<T>; otherwise, false.
ContainsKey(TKey)
Determines whether the System.Collections.Generic.IDictionary<TKey, TValue> contains an element with the specified key.
public bool ContainsKey(TKey key)
Parameters
keyTKeyThe key to locate in the System.Collections.Generic.IDictionary<TKey, TValue>.
Returns
- bool
true if the System.Collections.Generic.IDictionary<TKey, TValue> contains an element with the key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.
CopyTo(KeyValuePair<TKey, TValue>[], int)
Copies the elements of the System.Collections.Generic.ICollection<T> to an System.Array, starting at a particular System.Array index.
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
Parameters
arrayKeyValuePair<TKey, TValue>[]The one-dimensional System.Array that is the destination of the elements copied from System.Collections.Generic.ICollection<T>. The System.Array must have zero-based indexing.
arrayIndexintThe zero-based index in
arrayat which copying begins.
Exceptions
- ArgumentNullException
arrayis null.- ArgumentOutOfRangeException
arrayIndexis less than 0.- ArgumentException
The number of elements in the source System.Collections.Generic.ICollection<T> is greater than the available space from
arrayIndexto the end of the destinationarray.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<TKey, TValue>>
A System.Collections.Generic.IEnumerator<T> that can be used to iterate through the collection.
GetOrAdd(TKey, Func<TKey, TValue>)
Returns the value stored under the key or, if there is none, creates it with create and adds it,
atomically under the dictionary lock.
public TValue GetOrAdd(TKey key, Func<TKey, TValue> create)
Parameters
keyTKeyThe key of the value.
createFunc<TKey, TValue>Creates the value for a missing key; called under the lock.
Returns
- TValue
The existing or the created value.
GetOrAdd(TKey, Func<TKey, TValue>, out bool)
Returns the value stored under the key or, if there is none, creates it with create and adds it.
The lookup, the creation and the addition run under one lock, so concurrent callers never create two values for one key;
create blocks every other caller of this dictionary while it runs; it must not wait for threads that use the dictionary.
Added in ATAS 8.0.15.
public TValue GetOrAdd(TKey key, Func<TKey, TValue> create, out bool isNew)
Parameters
keyTKeyThe key of the value.
createFunc<TKey, TValue>Creates the value for a missing key. Its exception propagates and nothing is added.
isNewbooltrue if the value was created by this call; false if it already existed.
Returns
- TValue
The existing or the created value.
Remove(KeyValuePair<TKey, TValue>)
Removes the first occurrence of a specific object from the System.Collections.Generic.ICollection<T>.
public bool Remove(KeyValuePair<TKey, TValue> item)
Parameters
itemKeyValuePair<TKey, TValue>The object to remove from the System.Collections.Generic.ICollection<T>.
Returns
- bool
true if
itemwas successfully removed from the System.Collections.Generic.ICollection<T>; otherwise, false. This method also returns false ifitemis not found in the original System.Collections.Generic.ICollection<T>.
Exceptions
- NotSupportedException
The System.Collections.Generic.ICollection<T> is read-only.
Remove(TKey)
Removes the element with the specified key from the System.Collections.Generic.IDictionary<TKey, TValue>.
public bool Remove(TKey key)
Parameters
keyTKeyThe key of the element to remove.
Returns
- bool
true if the element is successfully removed; otherwise, false. This method also returns false if
keywas not found in the original System.Collections.Generic.IDictionary<TKey, TValue>.
Exceptions
- ArgumentNullException
keyis null.- NotSupportedException
The System.Collections.Generic.IDictionary<TKey, TValue> is read-only.
Remove(TKey, out TValue)
Removes the element with the specified key and returns its value.
public bool Remove(TKey key, out TValue value)
Parameters
keyTKeyThe key of the element to remove.
valueTValueThe removed value, or the default value of
TValueif the key was not found.
Returns
- bool
true if the element was found and removed; otherwise, false.
TryAdd(TKey, TValue)
Adds the value if the key is not present yet; an existing value is left unchanged.
public bool TryAdd(TKey key, TValue value)
Parameters
keyTKeyThe key of the element to add.
valueTValueThe value to add.
Returns
- bool
true if the value was added; false if the key already exists.
TryGetAndRemove(TKey)
Removes the element with the specified key and returns its value.
Added in ATAS 8.0.15.
public TValue? TryGetAndRemove(TKey key)
Parameters
keyTKeyThe key of the element to remove.
Returns
- TValue
The removed value, or the default value of
TValueif the key was not found.
TryGetValue(TKey)
Gets the value associated with the specified key.
Added in ATAS 8.0.15.
public TValue? TryGetValue(TKey key)
Parameters
keyTKeyThe key whose value to get.
Returns
- TValue
The value, or the default value of
TValueif the key is not found; for value types a missing key cannot be told from a stored default.
TryGetValue(TKey, out TValue?)
Gets the value associated with the specified key.
Added in ATAS 8.0.15.
public bool TryGetValue(TKey key, out TValue? value)
Parameters
keyTKeyThe key whose value to get.
valueTValueWhen this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the
valueparameter. This parameter is passed uninitialized.
Returns
- bool
true if the object that implements System.Collections.Generic.IDictionary<TKey, TValue> contains an element with the specified key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.