Search

C# Entry Point

Every program must start and end somewhere. When you run a program the system looks for a method called Main and the contained code will be executed. When the code has finished running it will exit the method and the program will end. When you create a new executable window C# project, a Main method is created automatically in a class called Program. The main method must be marked as static and can exist anywhere in your project.

namespace MyProgram
{
    class Program
    {
        static void Main()
        {

            // your code goes here...

        }
    }
}

 

If your program needs to be supplied with initial values you can specify a string array parameter or alternatively you can use the GetCommandLineArgs() method.

namespace MyProgram
{
    class Program
    {
        static void Main(string[] args)
        {

            // your code goes here...

        }
    }
}

 

Further reading Main() and command-line arguments