int Main()

Posted on 2005-04-11
Last Modified: 2010-04-16

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

Question by:Kokas79
    LVL 4

    Expert Comment

    by default Main() is void. which int Main() are you referring to?
    LVL 20

    Accepted Solution

    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)
    LVL 10

    Expert Comment

    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.
    LVL 20

    Expert Comment

    @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.
    LVL 10

    Expert Comment

    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.
    LVL 1

    Expert Comment

    since main() is a static method you can make use of its value from another class before intializing the class containing the 'int main()'.

    Author Comment

    Thanks for your all the console applicatios i work on, Main() is used to execute the program and then all come to an 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.

    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?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now