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#?
Submitted by Nidhi, on September 11, 2020 [Last updated : March 17, 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 »


ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.