为什么一个类实现IDisposable明确,而不是含蓄? [英] Why would a class implement IDisposable explicitly instead of implicitly?

查看:126
本文介绍了为什么一个类实现IDisposable明确,而不是含蓄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的是 FtpWebResponse 类,并没有看到Dispose方法。 <一href=\"http://stackoverflow.com/questions/3118861/how-does-this-class-implement-idisposable-if-it-doesnt-have-a-dispose-method\">It原来的这个类实现IDisposable,但这样做明确的,这样你必须先投你的实例IDisposable接口调用Dispose前:

I was using the FtpWebResponse class and didn't see a Dispose method. It turns out that the class implements IDisposable, but does so explicitly so that you must first cast your instance to IDisposable before calling Dispose:

// response is an instance of FtpWebResposne
((IDisposable) response).Dispose();

为什么会一类的,如这一个设计师选择明确实现IDisposable?作为<一个href=\"http://stackoverflow.com/questions/3118861/how-does-this-class-implement-idisposable-if-it-doesnt-have-a-dispose-method/3118890#3118890\">Anthony Pegram说,做事这种方式掩盖该对象应为处理每一个谁,他/她用上课的时间没有咨询文件的普通开发人员的事实。

Why would the designer of a class such as this one choose to implement IDisposable explicitly? As Anthony Pegram says, doing things this way masks the fact that the object should be disposed for the average developer who is not consulting the documentation every time he/she uses a class.

推荐答案

如果班里有一个关闭方法是完全一样的,因为这是正常执行的Dispose 。原来的Dispose 是隐藏在一个明确的实施,使完全相同的方法没有两个名字。

This is normally done if the class has a Close method that is the exact same as Dispose. The original Dispose is hidden in an explicit implementation so that the exact same method doesn't have two names.

它的官方建议在这里:

<一个href=\"http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae\">Do如果这样的术语是标准的,例如作为与文件或插座实施清理目的的关闭方法。这样做的时候,建议您关闭执行相同的处置......

考虑实现接口成员明确隐藏的成员,并具有更好的名称添加对等成员。

偶尔特定的域名比的Dispose更合适。例如,一个文件封装可能希望使用的方法名称关闭。在这种情况下,私下实施处置,并创建调用Dispose公共关闭的方法。

(P.S。我不同意这个约定。)

(P.S. I disagree with this convention.)

这篇关于为什么一个类实现IDisposable明确,而不是含蓄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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