Open In App

Console.OpenStandardOutput() Method in C# with Examples

Last Updated : 22 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads