Sep 20, 2011

Return type of Main method in C#

In c sharp Main method can be return void as well as int. For example:

Main method returning void:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("exacthelp.blogspot.com");
            Console.ReadKey();
        }
    }
}

Output:
exacthelp.blogspot.com

Main method returning int:

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static int Main(string[] args)
        {
            Console.WriteLine("exacthelp.blogspot.com");
            Console.ReadKey();

            return 0;
        }
    }
}

Output:
exacthelp.blogspot.com

Why Main method in c# should return a value?

It is good practice to return an integer value from Main method. Actually Main method returns this value to operating system which indicates the program execution status.
It is convention, if Main method returns zero it means program has executed successfully and it is non-zero means program has ended with error.
Value returned by Main method is set to the ERRORLEVEL of environment variable of operating system. In general ERRORLEVEL returns an integer value i.e. status value of last executed program in operating system.
Demo work:
We will create a simple console application and execute this exe file by a batch program and display the success or failure message by checking the environment variable ERRORLEVEL.

Step1:
Create a console application project in visual studio and named it CheckProgramStatus.

Step2: Write following code and build it.

using System;

namespace CheckProgramStatus
{
    class Program
    {
        static int Main(string[] args)
        {
            int dividend = 20;
            int divisor;
            int quotient;

            divisor = Convert.ToInt32(args[0]);

            quotient = dividend / divisor;
            Console.WriteLine("Quotient is: {0}", quotient);

            return 0;
        }
    }
}

Step3:
Create a batch file. For this open a notepad and write the following code:
@echo off
CheckProgramStatus.exe %1
@if "%ERRORLEVEL%" == "0" goto success
:failed
    echo Program ended with error.
    echo Return value of Main method = %ERRORLEVEL%
    goto end
:success
    echo Program has executed successfully.
    echo Return value of Main method = %ERRORLEVEL%
    goto end
:end

Step4: Save the notepad as: ProgramExecutionTester.bat with save type as: All files

Step5: Paste the both files CheckProgramStatus.exe and ProgramExecutionTester.bat in C drive.

Step6: Open the command prompt and in c drive write:
C:\>ProgramExecutionTester.bat 10
You will get following output:
Quotient is: 2
Program has executed successfully.
Return value of Main method = 0

Step7: In command prompt now write:
C:\>ProgramExecutionTester.bat 0
You will get output like this:
Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero.
   at CheckProgramStatus.Program.Main(String[] args)
Program ended with error.
Return value of Main method = -1073741676
Note: If OS prompted any message box close it.

So by returning an integer value from Main method we can know the status of program execution.


1 comment: