using System; using System.IO; public class Program { public static void Main(string[] args) { string filename = "output.txt"; using (StreamWriter writer = new StreamWriter(filename)) { writer.WriteLine("Hello, world!"); writer.WriteLine("This is a test."); writer.WriteLine("Goodbye."); } } }
using System; using System.IO; public class Program { public static void Main(string[] args) { Console.SetOut(new StreamWriter(Console.OpenStandardOutput())); Console.WriteLine("Hello, world!"); Console.WriteLine("This is a test."); Console.WriteLine("Goodbye."); } }In this example, we redirect the standard output stream to a new `StreamWriter` object. This allows us to use the `WriteLine` method to write text to the console, rather than to a file. These examples use the `System.IO` package library, which is included as part of the .NET Framework.