[Solved] C# Dictionary is empty after calling new function


Your code seams ok but you have to protect your dictionary from external access. I think the root cause of your problem is this.

Change your code

private readonly Dictionary<string, double> dict = new Dictionary<string, double>();

Also modify the getValue() method like this:

public override Object getValue()
{
    return dict.ToDictionary(m => m.Key, m => m.Value);
}

This will give back a snapshot from your dictionary and protect your original one.
It has a little overhead for GC (extra dictionary creation) but it is negligible.

0

solved C# Dictionary is empty after calling new function