I have a few classes that hold references to other classes through IDictionary instance members.
Like so:
class A
{
private readonly Dictionary<int, B> _particles = new Dictionary<int, B>();
public void CreateNewB(int someInt)
{
var b = new B();
if (!_particles.ContainsKey(someInt)
_particles.Add(someInt, b);
}
}
so this is the setup, and I NEVER remove them from this dictionary, but for some reason, the destructor for class B gets called on a GC run every now and then and I dont understand why.
Could it be something to do with how the Dictionary class adds new references?
FIXED:
Ok, thank you all for your answers, I have certainly gain a large understanding about the GC and deconstructors now.
But the issue was my own, I was adding someInt only if it did not exist already and through flawed business logic, someInt was always 1, so the first time through it worked and the deconstructors did not get called. But the second time though, the "b" instance was simply not added to the list and was cleaned up in the GC run.
Thanks again to all who helped out!