The Wayback Machine - https://web.archive.org/web/20240204104729/https://www.geeksforgeeks.org/console-openstandardoutput-method-in-c-sharp-with-examples/
Open In App
Related Articles

Console.OpenStandardOutput() Method in C# with Examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Console.OpenStandardOutput Method is used to get the standard output stream. There are two overloads of OpenStandardOutput method available in C# which are listed below:

  • OpenStandardOutput() Method
  • OpenStandardOutput(int32) Method

OpenStandardOutput() Method

It is used to get the standard output stream.

Syntax: public static System.IO.Stream OpenStandardOutput ();
Parameters: This method does not accepts any parameter.
Return value: This method returns the standard output stream.

Example:




// C# program to illustrate the
// Console.OpenStandardOutput()
// method
using System;
class GFG
{
    static void Main(string[] args)
    {
        // use of Console.OpenStandardOutput() method
        // to print the Geeksforgeeks
        // BeginWrite returns an IAsyncResult that represents
        //  the asynchronous write
        // int32 value like 071, 101 represents the 
        // ascii value of Geeksforgeeks
        if (System.Console.OpenStandardOutput().BeginWrite(new byte[] { 071, 101,101, 
            107,115, 102, 111, 114, 103, 101,101, 
            107,115, 0 },0, 
            13, null, null).AsyncWaitHandle.WaitOne()) 
        { 
        }
    }
}


Output:

Geeksforgeeks

OpenStandardOutput(Int32) Method

It is used to get the standard output stream, which is set to a specified buffer size.

Syntax:public static System.IO.Stream OpenStandardOutput (int bufferSize);
Parameters: This method accepts the following parameter.

  • bufferSize: This parameter is the internal stream buffer size.

Return value: This method returns the standard output stream.
Exception: This method will give ArgumentOutOfRangeException if the buffersize is less than or equal to zero.

Example: It replaces 2 consecutive space characters in a string with a tab character.




// C# program to illustrate the 
// Console.OpenStandardOutput(int32) method
using System;
using System.IO;
   
public class GFG
{
    // size of tab
    private const int Val1 = 2;
       
    // working string
    private const string Val_Text = "Geeks for Geeks";
   
    // main function
    public static int Main(string[] args)
    {
        // check for the argument
        if (args.Length < 2)
        {
            Console.WriteLine(Val_Text);
            return 1;
        }
   
        try
        {
            // replacing space characters in a string with
            // a tab character
            using (var wrt1 = new StreamWriter(args[1]))
            {
                using (var rdr1 = new StreamReader(args[0]))
                {
                    Console.SetOut(wrt1);
                    Console.SetIn(rdr1);
                    string line;
                    while ((line = Console.ReadLine()) != null)
                    {
                        string newLine = line.Replace(("").PadRight(Val1, ' '), "\t");
                        Console.WriteLine(newLine);
                    }
                }
            }
        }
        catch(IOException e)
        {
            TextWriter errwrt = Console.Error;
            errwrt.WriteLine(e.Message);
            return 1;
        }
          
        // use of OpenStandardOutput() method
        var standardOutput = new StreamWriter(Console.OpenStandardOutput());
        standardOutput.AutoFlush = true;
          
        // set the output
        Console.SetOut(standardOutput);
        Console.WriteLine("OpenStandardOutput Example");
        return 0;
    }
}


Output:

Geeks for Geeks

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 22 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials