需要明确与一次性 [英] Need Clarity with Idisposable

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

问题描述

为了处理对象,首先必须从Idisposable,.....
继承引用该对象的类.
但是一旦从Idisposable继承了一个类,但是如果未在对象means上显式调用dispose()方法,该对象将被处置吗?
IDisposable的用途是什么,如果从IDisposable继承但未使用Dispose()的类呢?

In order to Dispose an object , first the class referenced to that object must be inherited from Idisposable,.....

but once a class is inherited from Idisposable but If dispose() method is not explicitly called on object means , will that object gets disposed????

what is the use of IDisposable if class inherited from IDisposable but Dispose() is not used???

推荐答案

是的,最终-IDisposable br/>
如果不对实现IDisposable的对象使用Dispose,则当垃圾回收器意识到不再需要它并开始删除它时,框架将自动调用它.

注意:这与对象掉出范围的时间不同!垃圾收集器可能在数小时,数天甚至数周之内都无法绕到您的对象上.如果创建任何实现IDisposable的对象,则您有责任正确处理该对象,否则可能会出现问题.一个简单的示例是打开一个文件以读取而不丢弃流对象.如果您的应用程序稍后尝试写入文件,它将获得另一个应用程序正在使用的文件",因为流仍然存在(尽管它没有引用),并且将保留该文件,直到GC删除它为止.令人沮丧和烦恼!
Yes, it will - eventually.

If you do not use Dispose on an object that implements IDisposable, then it will be called automatically by the framework when the Garbage Collector realizes it is no longer needed and starts to delete it.

NOTE: This is not the same time as the object drops out of scope! The Garbage Collector may not get round to your object for hours, days or even weeks. If you create any object that implements IDisposable, then you are responsible for disposing of the object correctly, or problems can be expected. A simple example is to open a file for reading and not dispose of the stream object. If your application later tries to write to the file, it will get "File in use by another application" because the stream still exists (although it has no references) and is holding the file until the GC removes it. Frustrating, and annoying!


看看下面的链接.

IDisposable接口

IDisposable.Dispose方法

垃圾回收
Have a look at below links.

IDisposable Interface

IDisposable.Dispose Method

Garbage Collection


这篇关于需要明确与一次性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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