KGy SOFT

CacheTKey, TValueItem Property

KGy SOFT Core Libraries Help
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: 5.0.0-rc.1
Syntax

public TValue this[
	TKey key
] { get; set; }

Parameters

key
Type: TKey
Key of the element to get or set.

Return Value

Type: TValue
The element with the specified key.

Implements

IDictionaryTKey, TValueItemTKey
IReadOnlyDictionaryTKey, TValueItemTKey
Exceptions

Remarks

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.

Tip Tip
You can retrieve a thread-safe accessor by the GetThreadSafeAccessor method.

See Also

Reference