The Wayback Machine - https://web.archive.org/web/20240707025230/https://www.geeksforgeeks.org/main-method-in-c-sharp/
Open In App

Main Method in C#

Last Updated : 11 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • 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.


Similar Reads

Main Thread in C#
In C#, the Main method is the entry point of any console application. It is the first method that is executed when you run a C# program. Here's an example of a simple console application with a Main method: C/C++ Code using System; class Program { static void Main() { Console.WriteLine(&quot;Hello, world!&quot;); } } OutputHello, world! In this exa
5 min read
Difference between Method Overriding and Method Hiding in C#
Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called Method Overriding. In simple words, Overriding is a feature that allows a subclass or child class to provide a specific im
4 min read
C# | Uri.MakeRelativeUri(Uri) Method
Uri.MakeRelativeUri(Uri) Method is used to determine the difference between two Uri instances. Syntax: public Uri MakeRelativeUri (Uri uri); Here, it takes the URI to compare to the current URI. Return Value: If the hostname and scheme of this URI instance and uri are the same, then this method returns a relative Uri that, when appended to the curr
3 min read
Array.GetValue() Method in C# with Examples | Set - 1
Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows: Array.GetValue(Int32, Int32) Array.GetValue(Int64, Int64) Array.GetValue(Int32) Array.GetValue(Int64) Array.GetValue(Int32, Int32, Int32) Array.GetValue(Int64, Int
4 min read
C# | CharEnumerator.MoveNext() Method
CharEnumerator.MoveNext() Method is used to increments the internal index of the current CharEnumerator object to the next character of the enumerated string. Syntax: public bool MoveNext(); Return Value: This method returns the boolean value true value if the index is successfully incremented and within the enumerated string otherwise, false. Belo
2 min read
C# | Math.IEEERemainder() Method
In C#, IEEERemainder() is a Math class method which is used to return the remainder resulting from the division of a specified number by another specified number. Syntax: public static double IEEERemainder (double a, double b); Parameters: a: It is the dividend of type System.Double.b: It is the divisor of type System.Double. Return Type: This meth
2 min read
C# | Substring() Method
In C#, Substring() is a string method. It is used to retrieve a substring from the current instance of the string. This method can be overloaded by passing the different number of parameters to it as follows: String.Substring(Int32) Method String.Substring(Int32, Int32) Method String.Substring Method (startIndex) This method is used to retrieves a
3 min read
C# Program to Demonstrate the Use of Exit() Method of Environment Classhttps://origin.geeksforgeeks.org/?p=705454
Environment Class provides information about the current platform and manipulates, the current platform. The Environment class is useful for getting and setting various operating system-related information. We can use it in such a way that retrieves command-line arguments information, exit codes information, environment variable settings informatio
3 min read
C# | IndexOfAny() Method
In C#, IndexOfAny() method is a String Method. It is used to search the specified character index present in the string which will be the first occurrence from start index. It returns index as an integer value. This method can be overloaded by changing the number of arguments passed to it. IndexOfAny Method (Char[])IndexOfAny Method (Char[], Int32)
7 min read
C# | CharEnumerator.GetType() Method
CharEnumerator.GetType() Method is used to get the type of the current instance. This method is inherited from the Object Class. Syntax: public Type GetType(); Return Value: This method returns the exact runtime type of the current instance. Below are the programs to illustrate the use of CharEnumerator.GetType() Method:+ Example 1: // C# program t
2 min read
Article Tags :
three90RightbarBannerImg