本文介绍了每个用户定义的类是否需要实现IDisposable接口来获取垃圾回收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定用户定义的类对象是如何被垃圾回收的。我是否需要在每个类上实现IDisposable接口并调用dispose()方法来释放内存?解析方案

。当你停止引用它时,每个普通的托管.NET对象都会被垃圾回收。 IDisposable意味着你将实现一个需要被调用者调用的Dispose()方法 - 它通常会释放未被垃圾收集的东西。它还有助于确定释放内存的地方。



查看IDisposable模式以确保您的操作正确:




I am not sure how the user defined class objects are garbage collected. Do I need to implement IDisposable interface on every class and call the dispose() method on it to free the memory?

解决方案

No. Every normal managed .NET object gets garbage collected when you stop referring to it. IDisposable means that you you will implement a Dispose() method that needs to be called by the caller -- it usually releases things that aren't garbaged collected. It also helps with having a deterministic place to release memory.

Check out the IDisposable pattern to make sure you do it right:

http://www.atalasoft.com/cs/blogs/stevehawley/archive/2006/09/21/10887.aspx

这篇关于每个用户定义的类是否需要实现IDisposable接口来获取垃圾回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 20:21