CacheTKey, TValueEnsureCapacity Property

KGy SOFT Core Libraries Help
Gets or sets whether adding the first item to the cache or resetting Capacity on a non-empty cache should allocate memory for all cache entries.
Default value: , unless you use the IDictionaryTKey, TValue initializer constructor, which initializes this property to .

Namespace:  KGySoft.Collections
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.0.0

public bool EnsureCapacity { get; set; }

Property Value

Type: Boolean



If Capacity is large (10,000 or bigger), and the cache is not likely to be full, the recommended value is .

When EnsureCapacity is , the full capacity of the inner storage is allocated when the first item is added to the cache. Otherwise, inner storage is allocated dynamically, increasing its size again and again until the preset Capacity is reached. When increasing occurs the storage size is increased to a prime number close to the double of the previous storage size.

Note Note
When EnsureCapacity is , then after the last storage doubling the internally allocated storage can be much bigger than Capacity. But setting  to this property trims the possibly exceeded size to a prime value close to the actual capacity.

When cache is not empty and EnsureCapacity is just turned on, the cost of setting this property is O(n), where n is Count. In any other cases cost of setting this property is O(1).

See Also