当一个值(对象)从字典中删除时会发生什么

本文关键字:删除 什么 一个 对象 字典 | 更新日期: 2023-09-27 18:15:50

我对ObjectDictionary中移除时会发生什么有疑问。假设我有以下代码片段:

Dictionary<string, TestClass> classdictionary = new Dictionary<string, TestClass>();
testclass = new TestClass();
classdictionary.Add("1", testclass);
classdictionary.Remove("1");

现在testclass发生了什么?会由GC清理吗?

当一个值(对象)从字典中删除时会发生什么

只要有另一个引用,它就不会被清理。

如果你的字典保存了对它的最后一个引用,垃圾收集器将清理它。与往常一样,这不会是立即的,而是在将来的某个时候,GC算法感觉像是在工作。