从IDisposable继承 - 什么时候? [英] Inherit from IDisposable - when?

查看:56
本文介绍了从IDisposable继承 - 什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,


什么时候或者你不应该从IDisposable界面继承你的自定义类?


假设我有一个自定义类SomeClass,它可以做某些事情。如果我创建一个新的SomeClass实例并且它超出了范围,那么GC将始终清理

,对吧?那么,为什么我要经历从

IDisposable继承的麻烦,实现Dispose()和Dispose(bool处置)方法 -

什么时候没有区别?这有规则吗?什么时候需要?


简短的问题,但通常会产生很多讨论;)我正在寻找

转发回复! :)


谢谢,


Razzie

Hey all,

When should or should you not inherit from the IDisposable interface for
your custom classes?

Say I have a custom class SomeClass that does... some stuff. If I create a
new instance of SomeClass and it goes out of scope, the GC will always clean
that up, right? So, why should I undergo the trouble of inheriting from
IDisposable, implement the Dispose() and Dispose(bool disposed) methods -
when there is no difference? Are there rules to this? When is it necessary?

Short question, but usually generating a lot of discussions ;) I''m looking
forward to the reponses! :)

Thanks,

Razzie

推荐答案

Razzie< ra **** @ quicknet.nl>写道:
Razzie <ra****@quicknet.nl> wrote:
你应该或不应该从IDisposable接口继承你的自定义类?

说我有一个自定义类SomeClass做...一些东西。如果我创建一个新的SomeClass实例并且它超出范围,那么GC将始终清理它,对吧?那么,为什么我要经历从IDisposable继承的麻烦,实现Dispose()和Dispose(bool处置)方法 -
什么时候没有区别?这有规则吗?什么时候需要?

简短的问题,但通常会产生很多讨论;)我正在寻找转发的回应! :)
When should or should you not inherit from the IDisposable interface for
your custom classes?

Say I have a custom class SomeClass that does... some stuff. If I create a
new instance of SomeClass and it goes out of scope, the GC will always clean
that up, right? So, why should I undergo the trouble of inheriting from
IDisposable, implement the Dispose() and Dispose(bool disposed) methods -
when there is no difference? Are there rules to this? When is it necessary?

Short question, but usually generating a lot of discussions ;) I''m looking
forward to the reponses! :)




IDisposable适用于您的类的实例直接或间接

包含非托管资源,例如文件句柄。


绝大多数课程都不需要实现IDisposable。


-

Jon Skeet - < sk ***@pobox.com>
http://www.pobox.com /〜双向飞碟

如果回复小组,请不要给我发邮件



IDisposable is for when instances of your class directly or indirectly
contain unmanaged resources, such as file handles.

The vast majority of classes don''t need to implement IDisposable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


间接在这里非常关键。根据经验,如果您的类具有实现IDisposable的成员,那么您的类也应该实现IDisposable。这样可以在确定的时间点清理成员的资源,而不是依赖于最终确定以处理任何潜在的非托管资源。


问候


Richard Blewett - DevelopMentor
http:// staff .develop.com / richardb / weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/< MP *** *********************@msnews.microsoft.com>


Razzie< ra **** @ quicknet.nl>写道:
The "indirectly" is pretty crucial here. As a rule of thumb, if your class has members who implement IDisposable then your class should also implement IDisposable. This allows the member''s resources to be cleaned up at a determined point in time rather than relying on finalization to take care of any underlying unmanaged resources.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<MP************************@msnews.microsoft.com >

Razzie <ra****@quicknet.nl> wrote:
你应该或不应该从IDisposable接口继承你的自定义类?

说我有一个自定义类SomeClass做...一些东西。如果我创建一个新的SomeClass实例并且它超出范围,那么GC将始终清理它,对吧?那么,为什么我要经历从IDisposable继承的麻烦,实现Dispose()和Dispose(bool处置)方法 -
什么时候没有区别?这有规则吗?什么时候需要?

简短的问题,但通常会产生很多讨论;)我正在寻找转发的回应! :)
When should or should you not inherit from the IDisposable interface for
your custom classes?

Say I have a custom class SomeClass that does... some stuff. If I create a
new instance of SomeClass and it goes out of scope, the GC will always clean
that up, right? So, why should I undergo the trouble of inheriting from
IDisposable, implement the Dispose() and Dispose(bool disposed) methods -
when there is no difference? Are there rules to this? When is it necessary?

Short question, but usually generating a lot of discussions ;) I''m looking
forward to the reponses! :)




IDisposable适用于您的类的实例直接或间接

包含非托管资源,例如文件句柄。


绝大多数课程都不需要实现IDisposable。


-

Jon Skeet - < sk ***@pobox.com>
http://www.pobox.com /〜双向飞碟

如果回复小组,请不要给我发邮件


---

接收邮件已通过无病毒认证。

由AVG反病毒系统检查( http://www.grisoft.com)

版本:6.0.769 /病毒库:516 - 发布日期:24/09/2004


[microsoft.public.dotnet.languages.csharp]



IDisposable is for when instances of your class directly or indirectly
contain unmanaged resources, such as file handles.

The vast majority of classes don''t need to implement IDisposable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.769 / Virus Database: 516 - Release Date: 24/09/2004

[microsoft.public.dotnet.languages.csharp]


Richard Blewett [DevelopMentor]< ri ****** @ develop.com>写道:
Richard Blewett [DevelopMentor] <ri******@develop.com> wrote:
间接在这里非常关键。根据经验,如果你的班级有成员实施IDisposable,那么你的班级也应该实现IDisposable。这样可以在确定的时间点清理成员的资源,而不是依靠
最终确定来处理任何潜在的非托管资源。
The "indirectly" is pretty crucial here. As a rule of thumb, if your
class has members who implement IDisposable then your class should
also implement IDisposable. This allows the member''s resources to be
cleaned up at a determined point in time rather than relying on
finalization to take care of any underlying unmanaged resources.




是的。但请注意,除非你有*直接*包含非托管

资源,否则你可能不需要终结器 - 让直接容器的终结器直接容器照顾事情。


(例如,如果你想要一个终结器,那将是一个例外。
警告开发者他没有调用Dispose 。)


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复群组,请不要给我发邮件



Yup. Note, however, that unless you have *directly* contained unmanaged
resources, you probably don''t need a finalizer - let the finalizers of
the direct containers take care of things.

(One exception to this would be if you wanted to have a finalizer to
alert the developer that he''s failing to call Dispose.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于从IDisposable继承 - 什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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