一次性使用和管理资源 [英] IDisposable and managed resources

查看:123
本文介绍了一次性使用和管理资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

正确使用IDisposable接口

我有一个拥有托管和非托管资源的类。我正在使用IDisposable释放未管理的资源。我应该在dispose方法中释放托管资源吗?或者我可以让它发布到GC来释放托管资源? us / library / b1yfkh5e%28v = VS.100%29.aspxrel =nofollow> documentation ,你会发现这一行:


  • 释放类型在其Dispose方法中拥有的任何可用的资源。



所以在你的dispose方法中,你应该处理托管也实现IDisposable的资源。如果一个对象没有实现这个,你不必处理它。


Possible Duplicate:
Proper use of the IDisposable interface

I have a class that has both managed and unmanaged resources. I am using IDisposable to release un-managed resources. Should I release managed resources in the dispose method? Or I can leave it to GC to release managed resources?

解决方案

If you have a look at the following documentation you will find the line:

  • Free any disposable resources a type owns in its Dispose method.

So in your dispose method you should dispose of managed resources that also implement IDisposable. If an object doesn't implement this, you don't have to dispose of it.

这篇关于一次性使用和管理资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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