See the Remarks section for details and some examples.
KGySoft.CollectionsLockingCollectionKeyValuePairTKey, TValue
KGySoft.CollectionsLockingDictionaryTKey, TValue
Namespace: KGySoft.Collections
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.5.0-rc.1
[SerializableAttribute] public class LockingDictionary<TKey, TValue> : LockingCollection<KeyValuePair<TKey, TValue>>, IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable
Type Parameters
- TKey
- The type of the keys in the dictionary.
- TValue
- The type of the values in the dictionary.
The LockingDictionaryTKey, TValue type exposes the following members.
Name | Description | |
---|---|---|
![]() | LockingDictionaryTKey, TValue |
Initializes a new instance of the LockingDictionaryTKey, TValue class with a DictionaryTKey, TValue inside.
|
![]() | LockingDictionaryTKey, TValue(IDictionaryTKey, TValue) |
Initializes a new instance of the LockingDictionaryTKey, TValue class.
|
Name | Description | |
---|---|---|
![]() | Count |
Gets the number of elements contained in the LockingCollectionT.
(Inherited from LockingCollectionT.) |
![]() | IsReadOnly |
Gets a value indicating whether the LockingCollectionT is read-only.
(Inherited from LockingCollectionT.) |
![]() | Item |
Gets or sets the element with the specified key.
|
![]() | Keys |
Gets an ICollectionT containing the keys of the LockingDictionaryTKey, TValue.
|
![]() | Values |
Gets an ICollectionT containing the values of the LockingDictionaryTKey, TValue.
|
Name | Description | |
---|---|---|
![]() | Add(T) |
Adds an item to the LockingCollectionT.
(Inherited from LockingCollectionT.) |
![]() | Add(TKey, TValue) |
Adds an element with the provided key and value to the LockingDictionaryTKey, TValue.
|
![]() | Clear |
Removes all items from the LockingCollectionT.
(Inherited from LockingCollectionT.) |
![]() | Contains |
Determines whether the LockingCollectionT contains a specific value.
(Inherited from LockingCollectionT.) |
![]() | ContainsKey |
Determines whether the LockingDictionaryTKey, TValue contains an element with the specified key.
|
![]() | CopyTo | (Inherited from LockingCollectionT.) |
![]() | GetEnumerator |
Returns an enumerator that iterates through the collection.
(Inherited from LockingCollectionT.) |
![]() | Lock |
Locks the access of the underlying collection from other threads until Unlock is called as many times as this method was called. Needed to be called if
multiple calls to the wrapped collection have to be combined without releasing the lock between each calls.
(Inherited from LockingCollectionT.)See the Remarks section of the LockingCollectionT class for details and some examples. |
![]() | Remove(T) |
Removes the first occurrence of a specific object from the LockingCollectionT.
(Inherited from LockingCollectionT.) |
![]() | Remove(TKey) |
Removes the element with the specified key from the LockingDictionaryTKey, TValue.
|
![]() | TryGetValue |
Gets the value associated with the specified key.
|
![]() | Unlock |
When called as many times as Lock was called previously, then unlocks the access of the underlying collection so other threads also can access it.
(Inherited from LockingCollectionT.) |
Name | Description | |
---|---|---|
![]() | AddRangeKeyValuePairTKey, TValue | (Defined by CollectionExtensions.) |
![]() | AsThreadSafeKeyValuePairTKey, TValue |
Returns a LockingCollectionT, which provides a thread-safe wrapper for the specified collection.
This only means that if the members are accessed through the returned LockingCollectionT, then the inner state of the wrapped collection remains always consistent and not that all of the multi-threading concerns can be ignored.
(Defined by CollectionExtensions.)See the Remarks section of the LockingCollectionT class for details and some examples. |
![]() | Convert(Type, CultureInfo) | Overloaded.
Converts an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example. |
![]() ![]() | ConvertTTarget(CultureInfo) | Overloaded. (Defined by ObjectExtensions.) |
![]() | ForEachKeyValuePairTKey, TValue |
Similarly to the List<T>.ForEach method, processes an action on each element of an enumerable collection.
(Defined by EnumerableExtensions.) |
![]() | GetRandomElementKeyValuePairTKey, TValue(Boolean) | Overloaded.
Gets a random element from the enumerable source using a new FastRandom instance.
(Defined by EnumerableExtensions.) |
![]() | GetRandomElementKeyValuePairTKey, TValue(Random, Boolean) | Overloaded.
Gets a random element from the enumerable source using a specified Random instance.
(Defined by EnumerableExtensions.) |
![]() | In |
Gets whether item is among the elements of set.
(Defined by ObjectExtensions.)See the Examples section of the generic InT(T, T) overload for an example. |
![]() | IndexOf(FuncObject, Boolean) | Overloaded.
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions.) |
![]() | IndexOf(Object) | Overloaded.
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions.) |
![]() | IndexOfKeyValuePairTKey, TValue(FuncKeyValuePairTKey, TValue, Boolean) | Overloaded.
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions.) |
![]() | IndexOfKeyValuePairTKey, TValue(KeyValuePairTKey, TValue) | Overloaded.
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions.) |
![]() | IsNullOrEmpty | Overloaded.
Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions.) |
![]() | IsNullOrEmptyKeyValuePairTKey, TValue | Overloaded.
Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions.) |
![]() | JoinKeyValuePairTKey, TValue(String) | Overloaded.
Concatenates the items of the source collection into a new string instance
using the specified separator between the items.
(Defined by EnumerableExtensions.) |
![]() | JoinKeyValuePairTKey, TValue(Char) | Overloaded.
Concatenates the items of the source collection into a new string instance
using the specified separator between the items.
(Defined by EnumerableExtensions.) |
![]() | ShuffleKeyValuePairTKey, TValue | Overloaded.
Shuffles an enumerable source (randomizes its elements) using a new FastRandom instance.
(Defined by EnumerableExtensions.) |
![]() | ShuffleKeyValuePairTKey, TValue(Int32) | Overloaded.
Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions.) |
![]() | ShuffleKeyValuePairTKey, TValue(Random) | Overloaded.
Shuffles an enumerable source (randomizes its elements) using a specified Random instance.
(Defined by EnumerableExtensions.) |
![]() | ToCircularListKeyValuePairTKey, TValue |
Creates a CircularListT from an IEnumerableT.
(Defined by EnumerableExtensions.) |
![]() | TryAdd(Object, Boolean, Boolean) | Overloaded.
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryAddKeyValuePairTKey, TValue(KeyValuePairTKey, TValue, Boolean, Boolean) | Overloaded.
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryAddRange(IEnumerable, Boolean, Boolean) | Overloaded.
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions.) |
![]() | TryAddRangeKeyValuePairTKey, TValue(IEnumerableKeyValuePairTKey, TValue, Boolean, Boolean) | Overloaded.
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions.) |
![]() | TryClear(Boolean, Boolean) | Overloaded.
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions.) |
![]() | TryClearKeyValuePairTKey, TValue(Boolean, Boolean) | Overloaded.
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions.) |
![]() | TryConvert(Type, Object) | Overloaded. (Defined by ObjectExtensions.) |
![]() | TryConvert(Type, CultureInfo, Object) | Overloaded. (Defined by ObjectExtensions.) |
![]() | TryConvertTTarget(TTarget) | Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. |
![]() | TryConvertTTarget(CultureInfo, TTarget) | Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example. |
![]() | TryGetElementAt(Int32, Object, Boolean, Boolean) | Overloaded.
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions.) |
![]() | TryGetElementAtKeyValuePairTKey, TValue(Int32, KeyValuePairTKey, TValue, Boolean, Boolean) | Overloaded.
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions.) |
![]() | TryInsert(Int32, Object, Boolean, Boolean) | Overloaded.
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryInsertKeyValuePairTKey, TValue(Int32, KeyValuePairTKey, TValue, Boolean, Boolean) | Overloaded.
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryInsertRange(Int32, IEnumerable, Boolean, Boolean) | Overloaded.
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions.) |
![]() | TryInsertRangeKeyValuePairTKey, TValue(Int32, IEnumerableKeyValuePairTKey, TValue, Boolean, Boolean) | Overloaded.
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions.) |
![]() | TryRemove(Object, Boolean, Boolean) | Overloaded.
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryRemoveKeyValuePairTKey, TValue(KeyValuePairTKey, TValue, Boolean, Boolean) | Overloaded.
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions.) |
![]() | TryRemoveAt(Int32, Boolean, Boolean) | Overloaded.
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions.) |
![]() | TryRemoveAtKeyValuePairTKey, TValue(Int32, Boolean, Boolean) | Overloaded.
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions.) |
![]() | TryRemoveRange(Int32, Int32, Boolean, Boolean) | Overloaded.
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions.) |
![]() | TryRemoveRangeKeyValuePairTKey, TValue(Int32, Int32, Boolean, Boolean) | Overloaded.
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions.) |
![]() | TryReplaceRange(Int32, Int32, IEnumerable, Boolean, Boolean) | Overloaded.
Tries to remove count amount of items from the target at the specified index, and
to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions.) |
![]() | TryReplaceRangeKeyValuePairTKey, TValue(Int32, Int32, IEnumerableKeyValuePairTKey, TValue, Boolean, Boolean) | Overloaded.
Tries to remove count amount of items from the target at the specified index, and
to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions.) |
![]() | TrySetElementAt(Int32, Object, Boolean, Boolean) | Overloaded.
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions.) |
![]() | TrySetElementAtKeyValuePairTKey, TValue(Int32, KeyValuePairTKey, TValue, Boolean, Boolean) | Overloaded.
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions.) |
Type safety means that all members of the underlying collection are accessed in a lock, which only provides that the collection remains consistent as long as it is accessed only by the members of this class. This does not solve every issue of multi-threading automatically. Consider the following example:
var asThreadSafe = new LockingDictionary<MyKey, MyValue>(myDictionary); // Though both calls use locks it still can happen that two threads try to add the same key twice this way // because the lock is released between the two calls: if (!asThreadSafe.ContainsKey(myKey)) asThreadSafe.Add(myKey, myValue);
For the situations above a lock can be requested also explicitly by the Lock method, which can be released by the Unlock method. To release an explicitly requested lock the Unlock method must be called the same times as the Lock method. The fixed version of the example above:
var asThreadSafe = new LockingDictionary<MyClass>(myDictionary); // This works well because the lock is not released between the two calls: asThreadSafe.Lock(); try { if (!asThreadSafe.ContainsKey(myKey)) asThreadSafe.Add(myKey, myValue); } finally { asThreadSafe.Unlock(); }
To avoid confusion, the non-generic IDictionary interface is not implemented by the LockingDictionaryTKey, TValue class because it uses a different aspect of synchronization.
The GetEnumerator method and Keys and Values properties create a snapshot of the underlying collections so obtaining these members have an O(n) cost on this class.
![]() |
---|
Starting with .NET 4 a sort of concurrent collections appeared. While they provide good scalability for multiple concurrent readers by using separate locks for entries or for a set of entries, in many situations they perform worse than a simple locking collection, especially if the collection to lock uses a fast accessible storage (eg. an array) internally. It also may worth to mention that some members (such as the Count property) are surprisingly expensive operations on most concurrent collections as they traverse the inner storage and in the meantime they lock all entries while counting the elements. So it always depends on the concrete scenario whether a simple locking collection or a concurrent collection is more beneficial to use. |
![]() |
---|
For a CacheTKey, TValue use this class only if you want a thread-safe wrapper for all IDictionaryTKey, TValue members and if it is not a problem if the cache remains locked during the invocation of the item loader delegate passed to the appropriate constructor. Otherwise, it may worth to use an IThreadSafeCacheAccessorTKey, TValue instead, which can be obtained by the GetThreadSafeAccessor method. |