Namespace: KGySoft.Collections
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.5.0-rc.1
Parameters
- key
- Type: TKey
Key of the element to get or set.
Return Value
Type: TValueThe element with the specified key.
Implements
IDictionaryTKey, TValueItemTKeyIReadOnlyDictionaryTKey, TValueItemTKey
Exception | Condition |
---|---|
ArgumentNullException | key is null. |
KeyNotFoundException | The property is retrieved, the CacheTKey, TValue has been initialized without an item loader and key does not exist in the cache. |
Getting this property retrieves the needed element, while setting adds a new item (or overwrites an already existing item). If this CacheTKey, TValue instance was initialized by a non- item loader, then it is enough to use only the get accessor because that will load elements into the cache by the delegate instance that was passed to the constructor. When the cache was initialized without an item loader, then getting a non-existing key will throw a KeyNotFoundException.
If an item loader was passed to the constructor, then it is transparent whether the returned value of this property was in the cache before retrieving it. To test whether a key exists in the cache, use the ContainsKey method. To retrieve a key only when it already exists in the cache, use the TryGetValue method.
When the CacheTKey, TValue is full (that is, when Count equals to Capacity) and a new item is added, an element (depending on Behavior property) will be dropped from the cache.
If EnsureCapacity is , getting or setting this property approaches an O(1) operation. Otherwise, when the capacity of the inner storage must be increased to accommodate a new element, this property becomes an O(n) operation, where n is Count.
![]() |
---|
You can retrieve a thread-safe accessor by the GetThreadSafeAccessor method. |