The Main method
The programs in the previous lessons consisted of statements only. But in textbooks, at interviews and in other people's code, you will often meet the same program in another form—inside a class and a Main method.
Picture a cookbook with dozens of recipes. To cook dinner, the cook needs to know which page to start from, so a bookmark saying "start here" lies on that page. A C# program has such a bookmark too: the Main method.
Here is the "Hello, World!" program written the classic way:
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!"); // Hello, World!
}
}Let's go through it line by line:
class Programdeclares a class namedProgram. A class is a named container for code, and here it serves only as a wrapper forMain.- The curly braces
{and}mark a beginning and an end: everything between them belongs to the class. In C#, each such brace is customarily put on a line of its own. static void Main()is the header of theMainmethod.- The body of the method is the statements between its curly braces. Here there is one statement: the call to
Console.WriteLine.
The method and the entry point
A method is a named block of statements. The header static void Main() has four parts:
staticmeans the method belongs to the classProgramitself, and the runtime can call it right away without creating anything;voidmeans the method returns nothing; it only runs its statements;Mainis the method's name, always with a capital letter;()are empty parentheses: nothing is passed to the method when it is called.
The entry point is the method where a program's execution starts. In C# it is Main: the runtime finds it, runs its statements from top to bottom, and when they end, the program ends.
Here the cookbook bookmark stops working. You can put two bookmarks in a book, but a program has one entry point. If you declare Main in two classes, the compiler refuses to choose and reports error CS0017.
Top-level statements
Since C# 9, released in 2020, the wrapper can be left out. Statements written right in the file, outside any class or method, are called top-level statements. The one-line program Console.WriteLine("Hello, World!"); is written exactly that way.
The compiler itself builds a class Program and an entry-point method around such statements. That method is named <Main>$: such a name cannot be written in code, so it never clashes with yours. It works the same way as Main: the runtime calls it at startup.
There are three things to remember about top-level statements:
- they may appear in only one file of a program, since there is one entry point: a second such file gives error
CS8802; - in their file they come first, and a class declaration above them is error
CS8803; - they are an ordinary way to write code, not a special mode: the project that the
dotnet new consolecommand creates starts with aProgram.csfile of one line,Console.WriteLine("Hello, World!");.
Which form to choose
After compilation both forms give the same thing: a class with an entry-point method. Top-level statements are shorter, so they are chosen for small programs, and every example in this course is written that way. The classic Main appears in code written before 2020 and in projects where people want to see the entry point explicitly.
Interviewers often ask: "What are top-level statements, and what does the compiler turn them into?" A good answer names three facts:
- Since C# 9, the entry point need not be declared, and statements can be written right in the file.
- The compiler itself builds a class
Programand an entry-point method around them. - Such statements may appear in only one file of a program.
Common mistake
The method is named main with a small letter:
class Program
{
static void main() // ✗
{
Console.WriteLine("Hello, World!");
}
}error CS5001: Program does not contain a static 'Main' method suitable for an entry pointC# tells capital letters from small ones, so main is an ordinary method with a different name, and the compiler looks for the entry point only under the name Main. The word Program at the start of the message means the whole program, not the class: the text is the same whatever the class is called. People who wrote Java or C, where this method is called main, often make this mistake.
Key points
- The entry point is the method where execution starts; in C# it is
static void Main()inside a class. - Since C# 9, the entry point need not be declared: top-level statements are written right in the file.
- The compiler itself builds a class
Programand an entry-point method around top-level statements. - Top-level statements may appear in only one file of a program and come first in it.