Tutorialspoint.dev

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 identifer. [] 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:



  • The Main() method is the entry point a C# program from where the execution starts.
  • Main() method must be static because it is a class level method. To invoked without any instance of the class it must be static. Non-static Main() method will give a compile-time error.
  • Main() Method cannot be overridden because it is the static method. Also, the static method cannot be virtual or abstract.
  • Overloading of Main() method is allowed. But in that case, only one Main() method is considered as one entry point to start the execution of the program.

    Example: Valid Overloading of Main() Method

    // C# program to demonstrate the
    // Valid overloading of Main()
    // method
    using System;
      
    class GFG {
      
        // Main method
        // it can also be written as
        // static void Main(String []args)
        static void Main()
        {
            Console.WriteLine("Main Method");
        }
      
        // overloaded Main() Method
        static void Main(int n)
        {
            Console.WriteLine("Overloaded Main Method");
        }
      
        // overloaded Main() Method
        static void Main(int x, int y)
        {
            Console.WriteLine("Overloaded Main Method");
        }
    }

    Output:

    Main Method
    

    Warnings:

    prog.cs(17, 14): warning CS0028: `GFG.Main(int)’ has the wrong signature to be an entry point
    prog.cs(23, 14): warning CS0028: `GFG.Main(int, int)’ has the wrong signature to be an entry point

    Example: Invalid overloading of Main() Method

    // C# program to demonstrate the
    // Invalid overloading of Main()
    // method
    using System;
      
    class GFG {
      
        // Main method
        // it can also be written as
        // static void Main()
        static void Main(String[] args)
        {
            Console.WriteLine("Main Method");
        }
      
        // overloaded Main() Method
        static void Main()
        {
            Console.WriteLine("Overloaded Main Method");
        }
    }

    Compile Errors:

    prog.cs(11, 14): error CS0017: Program `5c56b8183078e496102b7f2662f8b84e.exe’ has more than one entry point defined: `GFG.Main(string[])’
    prog.cs(17, 14): error CS0017: Program `5c56b8183078e496102b7f2662f8b84e.exe’ has more than one entry point defined: `GFG.Main()’

  • The allowed type of command line arguments is only String array in the parameters of the Main() method.
  • The allowed return type of Main() method is void, int, Task(From C# 7.1), and Task<T>(From C# 7.1).
  • Declaration of Main() method may include async modifier if the return type of Main() method is Task or Task<T>.
  • Libraries and services in C# do not require a Main() method as an entry point.
  • If more than one C# class contains the Main() method then the user must have to compiler the program with /main option to specify which Main() method will be the entry point.


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter