Why does C# use implicit void Main?

You can use either int or void as a return type. Thus, simply change it and return a value like in C++.

Maybe it's void by default in order not to puzzle beginners.


In C#, you can use, see MSDN :

 static int Main() 
 static int Main(string[] args)
 static void Main() 
 static void Main(string[] args)

You can also return a (int) value in 2 ways.

In a Console application I would use int Main() { ...; return 2; }

In a WinForms/WPF/... app, in the rare situation it needs a return value, I would use
Environment.ExitCode = 1; or Environment.Exit(1);


It's not implicitly void. As in, you can't just declare main(String[] args) and have it compile as a void function. The default projects declare main() as void because the default projects don't have anything useful to return from main.

It's also worth noting that C# is not C or C++. Some of the syntax is the same, but the differences are far vaster.

Tags:

C#

.Net

Standards