using System.IO; string filepath = "example.txt"; if (File.Exists(filepath)) { using TextReader reader = File.OpenText(filepath); string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } }
using System.IO; string input; using TextReader reader = Console.In; while ((input = reader.ReadLine()) != "exit") { Console.WriteLine(input); }In both examples, TextReader is used to read text from a stream. The first example uses File.OpenText() to create a TextReader for reading a text file, while the second example uses Console.In to create a TextReader for reading input from the console.