IVlad
IVlad

Reputation: 43487

Why does C# use implicit void Main?

I don't understand why C#'s Main function is void by default (in a console project for example). In C and C++ the standard clearly says main must return int, and using a return value makes sense because we can check that return value from an external program and see if the C/C++ application finished successfully or encountered an error.

So my questions are:

  1. Why does Visual Studio declare Main as void?
  2. What's the best way of returning a value to the OS once a C# console application has finished executing?

Upvotes: 13

Views: 3726

Answers (8)

Henk Holterman
Henk Holterman

Reputation: 273571

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);

Upvotes: 12

tvanfosson
tvanfosson

Reputation: 532605

Best way to return an exit value is to use. I say this because non-zero exit codes are usually used to signify some error, typically one that you wouldn't want to continue from. Calling Exit with a non-zero exit value both signals the error through the value and exits the program.

Environment.Exit( value );

EDIT: Note, the context for my statement was within Main for a console program. Libraries and other methods should throw exceptions or return error values. Only your main routine should call Exit. Sorry if this wasn't clear.

Upvotes: 2

Sean
Sean

Reputation: 4470

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.

Upvotes: 7

Marc Wittke
Marc Wittke

Reputation: 3155

Remember, that there is still another "box" around your program - the CLR. It will return error codes, when your code throws an unhandled exception.

Upvotes: 2

Eric Petroelje
Eric Petroelje

Reputation: 60528

You can use Environment.Exit() to terminate and return an integer code to the OS. Alternatively, you can set Environment.ExitCode and that code will be returned to the OS whenever your program terminates normally.

Upvotes: 0

NebuSoft
NebuSoft

Reputation: 4000

The reasons behind why C#'s developers decided to have Main declared as void I couldn't say. As far as returning values to the OS when the application has finished you can use this:

System.Environment.ExitCode = 1; // Or whatever number other than 0 for errors.

Upvotes: 6

marr75
marr75

Reputation: 5725

You can change the return type of Main without causing any problems.

Upvotes: 3

Matthias
Matthias

Reputation: 12259

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.

Upvotes: 16

Related Questions