int Main()

Hello

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

Thanks
Kokas79Asked:
Who is Participating?
 
TheAvengerConnect With a Mentor Commented:
This is the method called when the application is started. Normally the IDE will write some code inside this method that will start your application (like showing a form for example). You can change the code to do whatever you need. The int result of the method is the result of your application. Other applications that call yours can get the result that you return in this method and thus know what your application did. For example a lot of DOS applications return 0 if everything was ok and a negative value when an error occurs (different values showing different errors)
0
 
imperial_p79Commented:
by default Main() is void. which int Main() are you referring to?
0
 
NetworkArchitekCommented:
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.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
TheAvengerCommented:
@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.
0
 
NetworkArchitekCommented:
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.
0
 
abhilashsomanCommented:
since main() is a static method you can make use of its value from another class before intializing the class containing the 'int main()'.
0
 
Kokas79Author Commented:
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?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.