Skip to content

C# Main method

C# Main Method
Become a developer with our complete learning paths
Become a developer with our complete learning paths

When you start learning C#, the first thing you will come across is the static void Main method. This method is used to define the entry point of a C# program. In this article, we will take a closer look at the static void Main method, and see how it works.

The static void Main method is the entry point of a C# program. It is where the execution of a C# program starts. The Main method is a static method, which means it can be called without creating an instance of the class. The Main method is also void, which means it does not return a value.

The Main method has the following signature:

static void Main(string[] args)

The Main method can take a parameter of type string array, which is used to pass command-line arguments to the Main method. The args parameter is optional, which means you can write a Main method without the args parameter.

When you write a C# program, you can put the Main method anywhere in the source code. However, it is a good practice to put the Main method in a separate class.

When the Main method is called, the execution of the program starts from the first line of code in the Main method. The Main method can call other methods, and those methods can call other methods, and so on. When the last line of code in the Main method is reached, the program ends.

Here is a simple C# program that prints the message “Hello, World!” to the console:

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

In this program, the Main method calls the WriteLine method of the Console class. The WriteLine method writes a line of text to the console.

You can also write a Main method that does not call any other methods. In this case, the program will simply end when the last line of code in the Main method is reached.

class Program
{
   static void Main(string[] args)
   {
      // This program does nothing
   }
}

You can also write a Main method that takes no parameters. In this case, you cannot access the command-line arguments from the Main method.

class Program
{
   static void Main()
   {
      // This program does nothing
   }
}

It is also possible to write a Main method that takes more than one parameter. However, this is not a common practice.

class Program
{
   static void Main(string[] args, int x)
   {
      // This program does nothing
   }
}

The Main method is a static method, which means it can be called without creating an instance of the class. However, it is also possible to write an instance method that can be used as the entry point of a C# program. In this case, you need to specify the method using the following attribute:

[STAThread]
public void Main()
{
   // This program does nothing
}

The STAThread attribute indicates that the Main method can be called from a single-threaded apartment. A single-threaded apartment is a type of threading model that is used by COM components.

The Main method is the entry point of a C# program, and it is where the execution of a C# program starts. The Main method is a static method, which means it can be called without creating an instance of the class. The Main method is also void, which means it does not return a value. The Main method can take a parameter of type string array, which is used to pass command-line arguments to the Main method.

And that is pretty much all you need to know about the famous C# Main method.

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide