Tan Jia Ming
Tan Jia Ming

Reputation: 465

What happens when a value (Object) was removed from Dictionary?

I have questions about what happens to the Object when they were removed from Dictionary. Lets say I have a following snippet:

Dictionary<string, TestClass> classdictionary = new Dictionary<string, TestClass>();

testclass = new TestClass();

classdictionary.Add("1", testclass);

classdictionary.Remove("1");

What happens to testclass now? Will it be cleaned up by GC?

Upvotes: 0

Views: 126

Answers (1)

nvoigt
nvoigt

Reputation: 77304

As long as you have another reference to it, it will not be cleaned up.

If your dictionary held the last reference to it, the garbage collector will clean it up. As always, that will not be immediate, but rather some time in the future the GC algorithm feels like doing work.

Upvotes: 3

Related Questions