Console.OpenStandardOutput() Method in C# with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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()) 
        
        }
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
    }
}

chevron_right


Output:

Geeks for Geeks



My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.