A structure that memoizes the result of some computation for later reuse. Maintains an internal dictionary to memoize results by key.

Namespace:  Gallio.Common
Assembly:  Gallio (in Gallio.dll) Version: 3.3.0.0 (3.3.459.0)

Syntax

C#
public struct KeyedMemoizer<TKey, TValue>
Visual Basic (Declaration)
Public Structure KeyedMemoizer(Of TKey, TValue)

Type Parameters

TKey
The key type.
TValue
The value type.

Remarks

Not thread safe.

Examples

CopyC#
public class MyClass
{
    // Do NOT put the "readonly" keyword on this field.
    // Otherwise we will not be able to modify the contents of the structure and memoization will not occur.
    private KeyedMemoizer<string, int> valueMemoizer = new KeyedMemoizer<string, int>();

    public int GetValue(string key)
    {
        return valueMemoizer.Memoize(key, () =>
        {
            // Expensive calculation here.
            return 42;
        });
    }
}

See Also