#8 – The Main() Function Can Return an int

Instead of returning void, Main() can return an integer value, which can then be read by the calling program or script.  Notice that the return type of the function is an int

class Program
{
    static int Main()
    {
        Console.WriteLine("Doing something..");

        if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)
            return -1;     // Monday is bad
        else
            return 0;
    }
}
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

7 Responses to #8 – The Main() Function Can Return an int

  1. S.K.C says:

    I did not understand the code

    return -1; // Monday is bad

    else

    return 0;

    can u tell me what does it do ???

    • Sean says:

      This example returns a -1 to the calling program if it is run on Monday, 0 otherwise. A value of 0 is often considered as “success” or “normal”, whereas a negative number is considered as an error. Whatever script or program invoked this application could then check the application’s return value.

      • S.K.C says:

        How could i check what is the return value is??
        when i debug..its doesnt give any info ..

      • Sean says:

        How you check it depends on how you invoke your program. If you call your program from a DOS batch file, you can use the ERRORLEVEL environment variable. If you launch your application from code, the API that you use to launch it should have the ability to retrieve the return value of the application that you launch. If you launch it from Windows Explorer, e.g. as a user, the return value is not reported. If you’re only debugging your program by having the debugger launch your program, it won’t show the return value of the program because the debugger has already terminated the session by the time the value is available.

      • Stephen York says:

        If you invoke an executable from another C# application, you are able to inspect the return values and respond accordingly.

  2. msuworld says:

    Can you please let know, on putting where console.Readkey() in this program wil show the return value as when i put it after console.writeline it only prints the sentence but doesn’t shows return value …

    • Sean says:

      Well, you could add a Write that display the value you were about to return, followed by a read so that the console paused. Just make sure to put both before the return statement(s) so that they are displayed before the program exits.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: