Is it sufficient to create an XmlWriter with a using block (with no call to Close()) or is it better to use a try/finally block and call Close() in finally?
The using block is a shortcut for a try/finally block with a call to Dispose() on any object that implements IDisposable.
In the case of streams and stream writers, Dispose() generally calls Close() manually. Using reflector, here's the Dispose method of XmlWriter:
protected virtual void Dispose(bool disposing)
{
if (this.WriteState != WriteState.Closed)
{
try
{
this.Close();
}
catch
{
}
}
}
So the short answer is yes, the using
block will handle closing the XmlWriter for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With