Friday, September 14, 2018

Entry Point Method Main

Entry Point Method:

In computer programming, an entry point is where control is transferred from the operating system to a computer program, at which place the processor enters a program or a code fragment and execution begins. The Main() method is the entry point of a C# console application or windows application. It is the starting point of execution of an application. When the application is started, the Main() method is the first method that is invoked. There can only be one entry point in a C# program. The Main() is declared inside a class. The Main() must be static. The Main() can either have a void or int as return type. The Main method can be declared with or without a string[] parameter that contains command-line arguments.

Syntax :-
public static void/int Main(String []args)
{
               -------------------
               -------------------
}

public:
Accessibility of a method is specified by access modifier. public is the access modifier of the main method that implies that the method is accessible from all other methods. The Main() method should be accessible to everyone. It has to be public so that .Net runtime can execute this method from outside the class. Remember that if you make any method non-public then it’s not allowed to be executed by any program. So it means that the main method has to be public.

static:
When program execution starts, there is no object of the class present. The Main method instantiates other objects and variables but there is no any method there that can instantiate the main method in C#. Static members are scoped to the class level, rather than the object level. Static members can be invoked without creating new class object. As main method is the entry point of the program it runs without creating an instance of the class. If the main method won’t be static, the runtime environment would not be able to call it because there is no object of the class is present.

void/int:
Every method must have a return type. In C# it is void or int. When it declare as void Main() does not return a value. Some time we can declare as int when it is declare as int operating system waits for return a value.

String []args:
            The Main method can be declared with or without a parameter. The String []args is array of string which is used as parameter. Sometime program needs to receive some arguments when execution stars. These arguments are called as command line argument. The String []args receives the command line arguments. It accepts only string type of argument and stores it in a string array.  

Valid forms of Main in C#:
There are four different ways to declare main method in C#.

1. public static void Main():
            This form of Main() will not return anything as it’s return type is void. It also not receives any command line argument.  

2. public static int Main():
             This form of Main() will returns integer value as it’s return type is int. An operating system waits for return a value. It will not receive any command line argument.

3. public static void Main( String []args):
            This form of Main() will not return anything as it’s return type is void. It will receives command line arguments in String []args.

4. public static int Main( String []args):
           This form of Main() will return integer to operating system as it’s return type is int. It will receives command line arguments in String []args.

2 comments:

  1. Sir type mistake ahe....it's return type is void is to be replaced with it's return type is int...last line I mean

    ReplyDelete