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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
So the short answer is yes, the
usingblock will handle closing the XmlWriter for you.