Link to home
Start Free TrialLog in
Avatar of Kokas79
Kokas79

asked on

int Main()

Hello

What is the purpose of Main() returning an int in C#? How do u use this feature?

Thanks
Avatar of imperial_p79
imperial_p79

by default Main() is void. which int Main() are you referring to?
ASKER CERTIFIED SOLUTION
Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well in C/C++ there is kind of a debate as to what the specification actually says on  that topic. On UNIX and UNIX-like environments  main() must return an int. I assume this may also be true for doing C# on *nix (with Mono or what have you). In Windows thought, well Windows doesn't care. Make it return 77 if you want or leave it void. If you have seen some code where it returned an int, well I think it can be attributed to the author being familiar with C/C++ where most consider it good practice to return an int.
@NetworkArchitek: this is not entirely true. In Windows the application also returns (or at least can return) a code. The calling process however must take care to make something in it. This has nothing to do with the language, you can return a value from C/C++, C# or even pure Pascal.
TheAvenger, yeah I realize that. I guess I wasn't clear. I was just making the point that you can do it however you want in Windows, of course if your needs require to return int then of course ... I was really getting at the idea in UNIX environments that returning int is required (unless there is a compiler flag that makes it unnecessary, not sure about that). I know there is a use for it ... Whenever I think of the "int main, void main" debate I always remember a time when I had to modify a lot of c++ programs to work on Linux and gcc was never happy with "void main." =)  I know nothing about Pascal so that is interesting, thanks.
since main() is a static method you can make use of its value from another class before intializing the class containing the 'int main()'.
Avatar of Kokas79

ASKER

Thanks for your comments....in all the console applicatios i work on, Main() is used to execute the program and then all come to an end....so for Main() to return void would be the logical thing to do. I was thinking that there is nothing after Main(), so why return an int if noone will use it? I guess TheAvenger's answer covers me, as if there is another application waiting for Main() to execute, then there is a reason for it to return an int. I never used DOS, so didnt know about that.

Also, i read that you can have many Main() methods in your code and you choose which one is the entry point to the application. So you can use the integer returned from the other Main() methods somehow...but that's a different story and sounds very complicated for me! If any of you have used this scenario then please me give me some info and i will rase the points.

<abhilashsoman>
you confused me a little bit...if there is only 1 Main() method in the application how can u do that? or you mean the scenario with many Main() functions?