Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hello World In C# without semicolon

Tags:

c#

console

Is it possible to write Hello World like in C without semicolon;?

In C:

  if(printf("Hello World!"))  //prints Hello World
  { 
  }

In C#:

//do stuff
like image 633
Javed Akram Avatar asked Dec 20 '25 12:12

Javed Akram


1 Answers

The trick is somehow constructing an expression from something that returns void. And luckily BeginInvoke does just that. Now we need to prevent the program from terminating before BeginInvoke wrote the text. Originally I just used a loop for that, but as SLaks showed we can use .AsyncWaitHandle.WaitOne() instead because it returns a bool.

Put this into the Main function:

if(((System.Action<string>)System.Console.WriteLine).BeginInvoke("Hello world",null,null).AsyncWaitHandle.WaitOne())
{
}
like image 55
CodesInChaos Avatar answered Dec 23 '25 02:12

CodesInChaos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!