Program does not contain a static 'main' method suitable for an entry point

posted 2 min read

If you are getting this error while trying to build your very first program in C#: "Program does not contain a static 'main' method suitable for an entry point", you've come to the right place. In this article, we’ll examine why this error can occur, followed by how we can fix it.

Reproducing the Entry Error

Main Entry Point errors occur when the compiler is not able to find a valid Entry point for the project you’re building. In order to reproduce the problem, we're going to create a small project and change the main function.

  1. Create a new C# console application by running the following command:

    dotnet new console -n Hello

  1. A new folder named Hello will be created in your file system. Go ahead and open in your favorite code editor/IDE. For this article, we're going to use vscode:
  1. Notice that we have a Program.cs file with no function inside. This is because we're using net8.0 which uses a minimal template. However we can rollback to the standard C# program layout:

  2. Run dotnet run. You'd notice that the console will print "Hello World":

  3. By default, the compiler searches for a class or struct that contains a function called Main defined within. Whenever its not found a signature with this specification, you may get the error CS5001: Program does not contain a static 'Main' method suitable for an entry point from the compiler. Now to visualize it, let's change the Main method and rename it to Hello:

  4. Running dotnet run again we'll receive the error CS5001 error:

.NET 6 new template

When .NET 6 came around, a new template was introduced with C# 10, where you don’t need to follow the Main function template anymore. Instead, you have a more direct approach, writing your code directly. Underneath the hood, the program will generate an entry point automatically.

Note that, the older options to manipulate entry points will not work anymore, such as -main options to select an alternative Main function.

The code below shows the new template implemented in recent versions of dotnet:

// See https://aka.ms/new-console-template for more information

System.Console.WriteLine($"Hello sum 1 + 2 = {Sum(1, 2)}");

double Sum(double a, double b)
{
      return a + b;
}

In the program above, we didn't have to declare an Entry Point, once all was done underneath the hood.

Conclusion

In this article we saw why the error CS5001 happens and what is needed by the compiler to find the right entry point. Providing the right Main function signature will ensure you don't get this error.

If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

Class is Not Abstract & Does Not Override Abstract Method - Fixed

Abdul Daim - Feb 24

Oserror: [winerror 10038] an operation was attempted on something that is not a socket

Mark Thompson - Nov 29, 2023

Checked exception is invalid for this method

prince yadav - Nov 26, 2023

Tips and Tricks in article formatting for Coderlegion

Phantom - Feb 9

Git and GitHub for Python Developers A Comprehensive Guide

Tejas Vaij - Apr 7
chevron_left