每个用户定义的类是否需要实现IDisposable接口来获取垃圾回收 [英] Does every user defined class need to implement IDisposable interface to get garbage collected

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

问题描述

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

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



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



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

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接口来获取垃圾回收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆