Why is XmlNodeList disposable?

╄→гoц情女王★ 提交于 2019-12-12 12:00:10

问题


I could not find an answer to this question.

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


回答1:


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();

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.



来源:https://stackoverflow.com/questions/14398798/why-is-xmlnodelist-disposable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!