Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlWriter: is calling Close() required if using a using block?

Tags:

c#

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?

like image 293
P a u l Avatar asked Apr 14 '10 18:04

P a u l


1 Answers

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.

like image 109
womp Avatar answered Oct 28 '22 12:10

womp