Open In App

Main Method in C#

C# applications have an entry point called Main Method. It is the first method which gets invoked whenever an application started and it is present in every C# executable file. The application may be Console Application or Windows Application. The most common entry point of a C# program is static void Main() or static void Main(String []args).

Different Declaration of Main() Method

Below are the valid declarations of Main Method in a C# program:



  1. With command line arguments: This can accept n number of array type parameters during the runtime.

    Example:




    using System;
      
    class GFG {
      
        // Main Method
        static public void Main(String[] args)
        {
      
            Console.WriteLine("Main Method");
        }
    }
    
    

    Output:

    Main Method

    Meaning of the Main Syntax:



    static: It means Main Method can be called without an object.
    public: It is access modifiers which means the compiler can execute this from anywhere.
    void: The Main method doesn’t return anything.
    Main(): It is the configured name of the Main method.
    String []args: For accepting the zero-indexed command line arguments. args is the user-defined name. So you can change it by a valid identifier. [] must come before the args otherwise compiler will give errors.

  2. Without Command line arguments: It is up to the user whether he wants to take command line arguments or not. If there is a need for command-line arguments then the user must specify the command line arguments in the Main method.

    Example:




    using System;
      
    class GFG {
      
        // Main Method
        static public void Main()
        {
      
            Console.WriteLine("Main Method");
        }
    }
    
    

    Output:

    Main Method
  3. Applicable Access Modifiers: public, private, protected, internal, protected internal access modifiers can be used with the Main() method. The private protected access modifier cannot be used with it.

    Example:




    using System;
      
    class GFG {
      
        // Main Method
        protected static void Main()
        {
      
            Console.WriteLine("Main Method");
        }
    }
    
    

    Output:

    Main Method

    Example:




    using System;
      
    class GFG {
      
        // Main Method
        private protected static void Main()
        {
      
            Console.WriteLine("Main Method");
        }
    }
    
    

    Compiler Error:

    More than one protection modifier specified

  4. Without any access modifier: The default access modifier is private for a Main() method.

    Example:




    using System;
      
    class GFG {
      
        // Main Method without any
        // access modifier
        static void Main()
        {
      
            Console.WriteLine("Main Method");
        }
    }
    
    

    Output:

    Main Method
  5. Order of Modifiers: The user can also swap positions of static and applicable modifiers in Main() method.

    Example:




    using System;
      
    class GFG {
      
        // Main Method
        public static void Main()
        {
      
            Console.WriteLine("Main Method");
        }
    }
    
    

    Output:

    Main Method

    Example:




    using System;
      
    class GFG {
      
        // Main Method with swapping of modifiers
        static internal void Main()
        {
      
            Console.WriteLine("Main Method");
        }
    }
    
    

    Output:

    Main Method
  6. Return Type: The Main Method can also have integer return type. Returning an integer value from Main() method cause the program to obtain a status information. The value which is returned from Main() method is treated as the exit code for the process.

    Example:




    using System;
      
    class GFG {
      
        // Main Method with int return type
        static int Main()
        {
      
            Console.WriteLine("Main Method");
      
            // for successful execution of code
            return 0;
        }
    }
    
    

    Output:

    Main Method

    Example:




    using System;
      
    class GFG {
      
        // Main Method with int return type
        static int Main(String[] args)
        {
      
            Console.WriteLine("Main Method");
      
            // for successful execution of code
            return 0;
        }
    }
    
    

    Output:

    Main Method

Important Points:


Article Tags :
C#