例如在这段代码中:
void ButtonCreator()
{
Button elboton = new Button();
}
Run Code Online (Sandbox Code Playgroud)
elboton我打电话给这个方法后会发生什么?
如果它们没有被其他对象(例如容器)引用,则它们变得无法访问并且有资格被垃圾收集器收集.这与创建任何其他对象相同.
请注意,System.Windows.Forms.Control类(及其子类Button)都实现了IDisposable接口,因此确保释放与之关联的任何非托管资源的最简单方法Button是使用using块,如下所示:
using(Button elboton = new Button()) {
// Do whatever you need to do
}
// elboton is now disposed
Run Code Online (Sandbox Code Playgroud)
但是,无论是否使用块,在GC运行之前都不会回收与该对象关联的任何受管资源using.
| 归档时间: |
|
| 查看次数: |
54 次 |
| 最近记录: |