Table of Contents

EvictingDictionaryPolicy Enum

Definition

Namespace
Bodu.Collections.Generic
Assembly
Bodu.Collections.dll
Package
Bodu.Collections 1.0.0
Source
EvictingDictionaryPolicy.cs

Specifies the eviction strategy used by an EvictingDictionary<TKey, TValue> when its capacity is exceeded.

public enum EvictingDictionaryPolicy
Extension Methods

Fields

FirstInFirstOut = 0

First-In, First-Out (FIFO): evicts the item that was added earliest, regardless of usage.

LeastRecentlyUsed = 1

Least Recently Used (LRU): evicts the item that has not been accessed for the longest time.

LeastFrequentlyUsed = 2

Least Frequently Used (LFU): evicts the item with the fewest total accesses.

MostRecentlyUsed = 3

Most Recently Used (MRU): evicts the most recently accessed item.

RandomReplacement = 4

Random Replacement: evicts a randomly selected item.

SecondChance = 5

Second Chance: evicts the first item found without a recent access flag, allowing items a second chance before removal.

Remarks

The EvictingDictionaryPolicy enumeration defines how items are selected for removal when the dictionary reaches its maximum capacity. The selected policy determines whether the dictionary behaves like a queue, cache, or frequency-based store.

Eviction Example:
Consider a dictionary with a capacity of 3. The following operations occur in order:

Add("A")    // Dictionary: A
Add("B")    // Dictionary: A, B
Add("C")    // Dictionary: A, B, C
Access("A") // Marks "A" as recently used or increases its frequency
Add("D")    // Triggers eviction: Dictionary must remove one entry

The item that is evicted varies depending on the selected EvictingDictionaryPolicy:

PolicyItem Evicted
FirstInFirstOut"A" - the first item added, regardless of access.
LeastRecentlyUsed"C" - the item least recently accessed (A was accessed, C was not).
LeastFrequentlyUsed"B" - the first item with the lowest access count.
MostRecentlyUsed"A" - the most recently accessed item.
RandomReplacementA randomly chosen item from "A", "B", or "C".
SecondChance "B" - evicted after its reference bit is cleared; "A" is spared due to recent access.

This example highlights the behavioral differences across policies using the same input sequence. Actual eviction order may vary depending on access patterns, dictionary configuration, and implementation details.

Applies to

ProductVersions
.NET8, 10