(C# Language) Main Method and Top Level Statements

Main method is the equivalent of the main method as we know in C language. It's the entry point of your program which means that it is the first function that executes when your program starts its execution. In this tutorial we learn the syntax of this function; we also learn about top-level statements that provide a shortcut to the main function.
(Rev. 31-Oct-2024)

Categories | About |     |  

Parveen,

Table of Contents (top down ↓)

Structure of a C# Program

We shall start from the same point at which we left the previous tutorial.

Let's first study the structure of a program.cs file. A namespace can be used to enclose your class in the same way as we have namespaces in C++. It has a name - like we have ConsoleApp1 - and the curly braces are the enclosures. These braces are not necessary as we shall see later.


// simplest program 

namespace ConsoleApp1
{
  class Program
  {
    static void Main()
    {
      Console.WriteLine("Hello, World!");
    }
  }
}

In C# functions exist inside a class.There are no global functions. So we have a class inside the namespace braces.

And inside this class we have the Main function. The function must be static. So we have static void Main. We have an output statement in this function.

The Main function can have many signature.

  1. static void Main
  2. static int Main - this one can return an int just like we have in C++.
  3. For asynchronous support we can have static async Task Main. we shall talk about async keyword later.
  4. We can also have static async Task<int> Main that returns an int.

Each of these functions have a corresponding overload that can take command line arguments Main (String [] args). The command line args are available as a string array.

The Main function need not be public. We can now run the program.

Video Explanation (see it happen!)

Please watch the following youtube video:

Run the Program

Open the debug menu and click "Start Debugging", or equivalently you can remember the shortcut key F5.

The program executes and prints the console message.

Simplifying the namespaces


// simplest program 

namespace ConsoleApp1
{
  class Program
  {
    static void Main()
    {
      Console.WriteLine("Hello, World!");
    }
  }
}

Suppose we have this program with the namespace that encloses this class. The new C# syntax allows us to remove the braces and replace them with a semi-colon as you see. The remaining code is automatically treated as if it were written inside namespace braces.


// the whole file gets this namespace 

namespace ConsoleApp1;

It is just a shortcut that you can make.

The program.cs file now looks like this. The braces of namespace have been removed.


// namespace brackets can be removed 

namespace ConsoleApp1;

class Program
{
  static void Main()
  {
    Console.WriteLine("Hello, World!");
  }
}

We can again run the program by using F5 key to verify that the program compiles successfully and runs the same way as earlier.

Top-Level Statements

Next let's see top level statements.

Main program can be simplified. There is no need to write the enclosing class, there is also no need to write the Main function.


// Main is implied 

Console.WriteLine("Hello, World!");

Console.WriteLine("More statements . . .");

You can write the statements of main function directly. Main is implied.

We can again run this file to verify that the program compiles and runs successfully.

If you have more statements, they can also be added. You can even create variables. The compiler will collect all these top level statements as if they had been written in a proper Main function.


// Main is implied 

Console.WriteLine("Hello, World!");

Console.WriteLine("More statements . . .");

// other classes and namespaces 
// can be added after all top level statements 
namespace MyNamespace
{
  class CMyClass
  {
    
  }
}

If you want to add more classes and namespaces, then they can be written after the top level statements. Here we have added a class to a namespace.

global namespaces

The Console class is in the namespace System. But the question is: where have we referenced the System namespace?

In the previous versions of C# we used to add using directives such as using System;

Click on the curly braces at the top left corner. Here we can see that visual studio maintains an autogenerated g.cs file that contains global namespace directives. We do not have to reference the namespaces in each file. It is automatically maintained in a global place.

We shall talk about the global keyword later also. Thanks!


This Blog Post/Article "(C# Language) Main Method and Top Level Statements" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.