How to print without using WriteLine() Method in C#?

Here, we are going to learn how to print a message without using the WriteLine() method in C#? By Nidhi Last updated : April 15, 2023

Printing Without Using WriteLine()

Here we will use Stream class to print a message on the console screen without using the WriteLine() method of Console class.

C# program to print a message without using the WriteLine() method

The source code to print a message without using the WriteLine() method is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to print a message without 
//using WriteLine() method

using System;
using System.Text;
using System.IO;

class Sample
{
    static void Main()
    {
        string str = "India";
        
        byte[] msg = Encoding.ASCII.GetBytes(str);

        Stream Ob = Console.OpenStandardOutput();
        Ob.BeginWrite(msg, 0,str.Length, null, null);

        Console.WriteLine();
    }
}

Output

India
Press any key to continue . . .

Explanation

In the above program, we created a class Sample that contains the Main() method. In the Main() method, we created a string str initialized with "India".

byte[] msg = Encoding.ASCII.GetBytes(str);

In the above statement, we converted the string into a byte array.

Stream Ob = Console.OpenStandardOutput();
Ob.BeginWrite(msg, 0,str.Length, null, null);

In the above statements, we created the object of Stream class and then write converted byte array on the standard output device that is "Monitor", that's why the message "India" will be printed on the console screen.

C# Basic Programs »


Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.