It is good practice to use using syntax whenever you work with an object that implements IDisposable (which StreamReader does), as it ensures that the Dispose method is always invoked and the object is correctly deleted.
For example, in this case, various descriptors / locks will be received in the "TestFile.txt" file, which may prevent other users from writing or even reading this file until the stream reader is removed or the process ends. Other objects (for example, database objects) can use database connections or network resources, and therefore you should always dispose of objects as soon as you use them - using the instructions is simple and safe to follow.
Under the covers, what happens is like this ( link ):
StreamReader sr = new StreamReader("TestFile.txt"); try { string line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } finally { if (sr != null) { ((IDisposable)sr).Dispose(); } }
However, the using statement is much cleaner (and less error prone) than trying to get rid of IDisposable manually.
source share