不使用 WriteLine 打印 Hello World 的 C# 程序
Hello World 程序是任何编程语言中最基本的程序。它在屏幕上打印“Hello world”。在本文中,我们将在不使用 WriteLine 方法的情况下显示“Hello World”。因此,要执行此任务,我们使用以下方法:
- Console.OpenStandardOutput( ):该方法用于获取标准输出流。
- Console.ReadKey() :此方法用于获取用户按下的下一个字符,此按键将显示在控制台窗口中。
- BeginWrite():此方法用于开始异步写入操作。
- AsyncWaitHandle.WaitOne():此方法用于等待异步操作完成。
为了编写 Hello World,我们以 ASCII 格式获取每个单独的字符,然后将这些字符一起显示。String H e l l o W o r l d ASCII Code 72 101 108 108 111 32 87 111 114 108 100
方法
1.在 if 条件中,编写 OpenStandardOutput() 以显示 Hello World。
2.该方法后面是 BeginWrite() 方法,该方法采用整数字节。
BeginWrite(new byte[] { 072, 101, 108, 108, 111, 032, 087, 111,
114, 108, 100, 0 }, 0, 12, null, null)
3.最后我们使用 AsyncWaitHandle.WaitOne() 方法,然后是 BeginWrite() 方法。
BeginWrite(new byte[] { 072, 101, 108, 108, 111, 032, 087, 111,
114, 108, 100, 0 }, 0, 12, null,
null).AsyncWaitHandle.WaitOne())
例子:
C#
// C# program to display Hello World without
// using WriteLine() method
using System;
class GFG{
static void Main(string[] args)
{
// ASCII values for Hello World
if (System.Console.OpenStandardOutput().BeginWrite(new byte[] {
072, 101, 108, 108, 111, 032, 087, 111, 114, 108, 100, 0 },
0, 12, null, null).AsyncWaitHandle.WaitOne())
{
}
}
}
输出:
Hello World