CacheTKey, TValue(FuncTKey, TValue, IEqualityComparerTKey) Constructor

Creates a new CacheTKey, TValue instance with the given itemLoader and comparer using default capacity of 128.

Definition

Namespace: KGySoft.Collections
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 10.0.0
C#
public Cache(
	Func<TKey, TValue>? itemLoader,
	IEqualityComparer<TKey>? comparer
)

Parameters

itemLoader  FuncTKey, TValue
A delegate that contains the item loader routine. This delegate is accessed whenever a non-cached item is about to be loaded by reading the indexer. If , then similarly to a regular DictionaryTKey, TValue, a KeyNotFoundException will be thrown on accessing a non-existing key.
comparer  IEqualityComparerTKey
The IEqualityComparerT implementation to use when comparing keys. If , EnumComparer<TEnum>.Comparer will be used for enum key types when targeting the .NET Framework, and EqualityComparer<T>.Default in other cases.

Remarks

Every key in a CacheTKey, TValue must be unique according to the specified comparer.

When CacheTKey, TValue is full (that is, when Count reaches Capacity) and a new element is about to be stored, then an element will be dropped out from the cache. The strategy is controlled by Behavior property.

If you want to add elements manually to the CacheTKey, TValue, then you can pass to the itemLoader parameter. In this case the CacheTKey, TValue can be used similarly to a DictionaryTKey, TValue: before getting an element, its existence must be checked by ContainsKey or TryGetValue methods, though Capacity is still maintained based on the strategy specified in the Behavior property.

See Also