tags:

views:

342

answers:

8

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?
+14  A: 

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.

winSharp93
It's void by default because you usually don't need to return anything from Main() in a win app (that return code is legacy from dos)
Pop Catalin
+3  A: 

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

marr75
Can you change it to anything you like? Or are `int` and `void` the only options?
Philip Potter
Well, the same can be said about C and C++ in some situations (at some point, `void main` was considered valid by some compilers). Is there a standard way of declaring Main in C#?
IVlad
+6  A: 

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.
NebuSoft
+7  A: 

In C#, you can use, see MSDN :

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

This flexibility (confusion) also exists in C++ and is inherited from C.

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

Henk Holterman
There is certainly confusion in C and C++, particularly in old textbooks, but the ISO standards of both C and C++ only require `int main(void)` and `int main(int, char**)`. `void main(void)` has never been standard and should be avoided.
Philip Potter
The answerer seems to be referring to what you can do, not the standard.
marr75
A: 

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.

Eric Petroelje
Is this considered a better practice than simply declaring Main as int and returning a value? I'm guessing it is, because this way we can exit the program with an exitcode from any other function.
IVlad
@IVlad - I wouldn't say it's a better practice really, just a different way of doing it.
Eric Petroelje
+1  A: 

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.

tvanfosson
-1: Calling `Environment.Exit()` is the worst kind of `goto` statement - from anywhere in the program, across all boundaries to termination.
280Z28
You miss the point -- Environment.Exit() is the ABEND of .NET. You call it exactly when you want to exit the program abnormally, with a non-zero exit value, precisely because continuing beyond that point would be dangerous (or pointless). It's a lot like an early return from a function. I can't think of any situation in which I would use it that would be better served by creating some bogus logic to get you to the "end" of the program. That said, I'd only use it in `Main` -- you don't want your libraries calling it, they should throw exceptions.
tvanfosson
+2  A: 

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

Marc Wittke
+6  A: 

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.

Sean Nyman