StreamReader reader = new StreamReader("file.txt"); //creating instance of StreamReader //...code to read from file... reader.Close(); //closing the reader instance
using(var reader = new StreamReader("file.txt")) { //...code to read from file... } //reader.Close() is called automatically due to using statementIn this example, we use the "using" statement to create an instance of StreamReader and automatically call its Close() method when the end of the "using" block is reached. Package library: System.Runtime or System.IO.Abstractions.