开发者

Dispose or not Dispose.. the writer created from XmlDocument thru navigator?

开发者 https://www.devze.com 2023-02-27 22:08 出处:网络
Do I really need to dispose the writer below? DataContractSerializer _serialier... var actual = new XmlDocument();

Do I really need to dispose the writer below?

DataContractSerializer _serialier...
var actual = new XmlDocument();
using (var writer = actual.CreateNavigator().AppendChild())
    _serialier.WriteObject(writer, myObj);

If not then the code is simplified to:

DataContractSerializer _serialier...
var actual = new XmlDocument();
_serialier.WriteObject(actual.CreateNavigator().AppendChild开发者_运维知识库(), myObj);


If the object implements IDisposable, then you should call Dispose on it when you're done.

If you don't do that, then your code is dependent on the assumption that you don't need to do it. What happens when your code is later refactored such that the XmlWriter being used is one that holds on to some resource?

0

精彩评论

暂无评论...
验证码 换一张
取 消