Console.SetOut() Method in C# Last Updated : 08 Nov, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report Console.SetOut(TextWriter) Method in C# is used to redirect the stream of standard output. With the help of this method, a user can specify a StreamWriter as the output object. The Console.SetOut method will receive an object of type TextWriter. The StreamWriter can be passed to Console.SetOut and it is implicitly cast to the TextWriter type. It simply sets the standard output stream property to the specified TextWriter object it gets. Syntax: public static System.IO.TextWriter Out { get; } or public static void SetOut (System.IO.TextWriter newOut); or public static void SetOut(TextWriter newOut) Return Value: It returns the streamWriter to specified TextWriter Object. Exceptions: When the newOut is null ArgumentNullException is thrown which does not accept it as a valid argument. When an I/O error occurred IOException is thrown. Example 1: csharp // C# code to demonstrate the use // of Console.SetOut method using System; using System.IO; class GFG { // Main Method static void Main() { // Creating a text file named "out" in D Drive using(StreamWriter writer = new StreamWriter("D:\\out.txt")) { Console.SetOut(writer); Result(); } } // Method Result static void Result() { // Writing to the file Console.WriteLine("GeeksforGeeks"); Console.WriteLine("A Computer Science portal for Geeks!"); } } Compiling and Executing: Output: Example 2: csharp // C# code to demonstrate the use // of Console.SetOut method using System; using System.IO; class GFG { // Main Method static void Main() { // will display on console Console.WriteLine("\nGeeksForGeeks"); // Creating a text file named "Geeks" // at the location of your program FileStream geeks1 = new FileStream("Geeks.txt", FileMode.Create); // Standard Output stream is // being saved to a Textwriter TextWriter geeksave = Console.Out; StreamWriter portal1 = new StreamWriter(geeks1); Console.SetOut(portal1); Console.WriteLine("\nThe Computer Science portal for Geeks"); Console.WriteLine("\nWelcome to GeeksforGeeks"); Console.SetOut(geeksave); // will display on console Console.WriteLine("This is Console.SetOut Method in C#"); Console.WriteLine("Get programming practices at your own pace !"); portal1.Close(); } } Compiling and Executing: Reference: https://docs.microsoft.com/en-us/dotnet/api/system.console.setout?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article Console.ResetColor() Method in C# M MerlynShelley Follow Improve Article Tags : C# CSharp-method CSharp-Console-Class Similar Reads Console.SetIn() Method in C# The Console.SetIn() method is used to set the In property of the specified StreamReader object i.e. it redirects the standard input from the console to the input file. Since the console is set with this StreamReader object, the ReadLine() method can be called to read the contents of the file, line b 2 min read Console.SetError() Method in C# The Console.SetError(TextWriter) Method sets the Error property of the specified StreamWriter i.e., it redirects the standard error stream to a file. As the console is set with this StreamWriter object, the WriteLine() method can be called to write the error into the file. Syntax: public static void 2 min read Console.ResetColor() Method in C# Console.ResetColor() Method is used to the foreground and background console colors to their defaults i.e. background to black and foreground to white.Syntax:Â Â public static void ResetColor (); Exceptions:Â Â SecurityException: If the user does not have permissions to perform the action.IOException: 1 min read Console.Read() Method in C# Console.Read() Method is used to read the next character from the standard input stream. This method basically blocks its return when the user types some input characters. As soon as the user press ENTER key it terminates. Syntax: public static int Read (); Return Value: It returns the next characte 1 min read Console.SetCursorPosition() Method in C# Console.SetCursorPosition(Int32, Int32) Method is used to set the position of cursor. Basically, it specifies where the next write operation will begin in the console window. The window origin changes automatically to make the cursor visible if the specified cursor position is outside the area that 1 min read Console.ReadKey() Method in C# Console.ReadKey() Method makes the program wait for a key press and it prevents the screen until a key is pressed. In short, it obtains the next character or any key pressed by the user. The pressed key is displayed in the console window(if any input process will happen). There are two methods in th 5 min read Like