Yes, there is a way to do this, but it has a drawback. Consider this class:
class Ref<T> { public T Value; }
You can use Dictionary<K, Ref<int>> dict
, and then do the following:
Ref<int> count; if (!dict.TryGetValue(key, out count)) { count = new Ref<int> { Value = 0 }; dict[key] = count; } count.Value += points;
The downside is that now you have an extra heap object to write to in the dictionary. Depending on your situation, this may or may not be acceptable.
source share