using System; using System.IO; using System.Threading.Tasks; class Program { static async Task Main() { using var reader = new StreamReader("example.txt"); string line; while ((line = await reader.ReadLineAsync()) != null) { Console.WriteLine(line); } } }
using System; using System.IO; using System.Net.Sockets; using System.Threading.Tasks; class Program { static async Task Main() { using var client = new TcpClient("example.com", 80); using var stream = client.GetStream(); using var reader = new StreamReader(stream); string line; while ((line = await reader.ReadLineAsync()) != null) { Console.WriteLine(line); } } }In this example, we use TcpClient object to connect to a network stream at example.com port 80. We use the GetStream method to get the stream object and use it to create a StreamReader object. We use the ReadLineAsync method to read lines from the stream asynchronously and print them to the console. Package/library: System.Runtime.Extensions.