There is a similar question. The problem that I'm having is that this defied the debugging system. The debugger created the problem.
So I have the following class:
public class Cache<TKey, TData> : Dictionary<TKey, TData>
{
public TData Get(TKey key, Func<TData> generate)
{
if (TryGetValue(key, out TData data))
{
return data;
}
try
{
Add(key, data = generate());
}
catch (Exception e)
{
Debugger.Break();
}
return data;
}
}
Now If I put a breakpoint in any of my generate
functions I get an exception. ArgumentException
An item with the same key has already been added.