Reputation: 2099
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?
Upvotes: 4
Views: 565
Reputation: 81189
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
.
Upvotes: 1