CacheTKey, TValueItem Property

Gets or sets the value associated with the specified key. When an element with a non-existing key is read, and an item loader was specified by the appropriate constructor, then the value is retrieved by the specified loader delegate of this CacheTKey, TValue instance.


Namespace: KGySoft.Collections
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 8.2.0
public TValue this[
	TKey key
] { get; set; }


key  TKey
Key of the element to get or set.

Return Value

The element with the specified key.


IDictionaryTKey, TValueItemTKey
IReadOnlyDictionaryTKey, TValueItemTKey


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.


ArgumentNullExceptionkey is null.
KeyNotFoundExceptionThe property is retrieved, the CacheTKey, TValue has been initialized without an item loader and key does not exist in the cache.

See Also