Skip to content

Main Method

When we were programming with C# in Part 1 we were using a language feature called Top Level Statements. This feature lets you program instructions without needing to explicitly create a Main method - the C# equivalent of main in C/C++.

Generally, these top level statements would only be used for small program scripts. Usually we would create a C# program that includes a Main method coded into a class.

The code for a Hello World program using a Main method is shown below.

namespace MyProject
{
public class MyProgram
{
public static void Main()
{
Console.WriteLine("Hello World");
}
}
}

Let’s have a look at this line by line:

  • namespace MyProject: C# programs have their code organised into namespaces. This code creates a namespace called MyProject that can be used to contain the code for the project. The following block ({...}) defines the scope of the namespace.
  • public class MyProgram: This declares a class within the MyProject namespace. We will look at classes in the next chapter.
  • public static void Main(): This declares a method named Main. This will be the entry point for the program, like the main function in C/C++. You cannot code methods outside of classes in C#.
  • Console.WriteLine("Hello World");: This calls the WriteLine method on the Console class, and outputs the message Hello World to the terminal.

This code shouldn’t look too strange, and we can connect this with things we have already learnt:

Once you get into a method things are very much the same as we coded before - you still use control flow statements to code the logic of the program. As methods exist within classes, these will be accessed as we saw in member functions.

For the moment, you can code up a C# program and code your logic in Main.

Other versions of Main

In C# there are several ways you can code main, as shown below. You can declare it as void if you don’t want to return a specific exit code to the operating system, or you can use int as the return type like in C/C++. Similarly, you can code it with no parameters or with an array of strings where you want to access any arguments passed to the program when it is run.

public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }

Most of the time you will use the public static void Main() { } version.