为什么XmlNodeList中一次性的?XmlNodeList

由网友(跩跩灬尒仯)分享简介:我无法找到这个问题的答案。I could not find an answer to this question.只是出于好奇,为什么用的的XmlNodeList类实现IDisposable的.NET 4.5,当它在previous版本没有?Just out of curiosity, why does the...

我无法找到这个问题的答案。

I could not find an answer to this question.

只是出于好奇,为什么用的的XmlNodeList类实现IDisposable的.NET 4.5,当它在previous版本没有?

Just out of curiosity, why does the XmlNodeList class implement IDisposable in .NET 4.5 when it didn't in the previous versions?

推荐答案

最有可能出于同样的原因是的IEnumerator< T> 工具 IDisposable的,但的IEnumerator 不 - 早期版本的作者认为的情况下,其中一个是一个实现可能需要清理作者过,但厂返还这类实现可能不知道这样的必要。例如,一个类可以接受的文件名,并提供了一​​个活的XmlNodeList 从该文件;在 IDisposable.Dispose 的XMLList 将关闭底层文件的方法。如果一个接口或抽象类用户的任何显著部分将不得不使用code这样的:

Most likely for the same reason that IEnumerator<T> implements IDisposable but IEnumerator does not--the earlier version was written before the authors thought of circumstances where one an a implementation might need cleanup, but a factory returning such an implementation might not know about such need. For example, a class might accept a file name and offer up a "live" XmlNodeList from that file; the IDisposable.Dispose method of the XmlList would close the underlying file. If any significant fraction of users of an interface or abstract class would have to use code like:

IDisposable asDispos = thing as IDisposable;
if (asDispos != null)
  asDispos.Dispose();

和如果许多那些不这样做,应该,那么事情也许应该实施的IDisposable 本身,因为它的速度更快无条件调用 IDisposable的.Dispose 上这是众所周知的实施的IDisposable ,比它试图铸造,可能会或可能不会实现类 IDisposable.Dispose 。

and if many of those that don't, should, then the thing should probably implement IDisposable itself, since it's faster to unconditionally call IDisposable.Dispose on a class which is known to implement IDisposable, than it is to try casting a class that may or may not implement IDisposable.Dispose.

阅读全文

相关推荐

最新文章