With the using construct, you call IDisposable.Dispose automatically at the end of the code block that closes the file. If you are not using the using statement, you should call Close.
With using you also automatically get built-in try / finally exception handling, which will behave more gracefully if something goes wrong once you leave the using block. This other reason using using is a good idea, not its own.
In your case, the using construct is a shorthand for:
StreamWriter file2 = new System.IO.StreamWriter(myFilePath); try { newFileContents.ForEach(file2.WriteLine); } finally { if (file2!= null) ((IDisposable)file2).Dispose(); }
If you decompile the StreamWriter Dispose implementation, you will see this call (among others):
this.stream.Close();
in a finally block (if you need absolute proof ;-)
source share