error CS5001: Program does not contain a static 'Main' method suitable for an entry point
means the compiler found nothing to start the program with: no correctly spelt
static Main method and no top-level statements. The usual culprits are a
lower-case main and a deleted or emptied Program.cs.
Spell it Main with a capital M and keep it static, or put at
least one top-level statement back in a .cs file of the project. Console
projects build into an executable, and an executable needs one place to start.
The error
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [...\FixLab.csproj]
Build FAILED.
The project path was shortened to ...; nothing else was changed. Note
that there is no file name or line number: the compiler (CSC) reports it for
the project as a whole, because the problem is something missing, not a wrong line.
Why it happens
The first program that produced it was copied from a Java habit:
namespace FixLab;
class Program
{
static void main(string[] args)
{
Console.WriteLine("Hello, David Chen!");
}
}
C# names are case-sensitive, and the entry point must be called exactly
Main. A method called main is just another private method, so
the build found no entry point. The other signatures C# accepts are variations of the
same name: static void Main(), static int Main(string[] args),
and static async Task Main(). Leaving out static is the
other near miss: void Main(string[] args) printed the same CS5001 line.
The second way to meet it is to clean up the template. With only a
Greeter.cs class left in the project and Program.cs deleted,
the same build printed the same CS5001 line. Since .NET 6 the template's
Program.cs holds top-level statements and no Main, so deleting
or emptying it removes the entry point without any visible Main going away.
The fix
namespace FixLab;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, David Chen!");
}
}
One capital letter: the project built with warnings treated as errors and
dotnet run printed Hello, David Chen!. For the deleted-file case,
add a Program.cs back with at least one statement, such as
Console.WriteLine(Greeter.Hello("Maria Garcia"));.
If the project is really meant to be a library that other projects reference, it does not
need an entry point at all: create it with dotnet new classlib, or change
<OutputType>Exe</OutputType> to
<OutputType>Library</OutputType> in the project file (the
Greeter-only project then built without errors) instead of adding an empty
Main.
How it was reproduced
A dotnet new console project on .NET SDK 10.0.403 (net10.0, nullable and
implicit usings on, no packages). Case one: Program.cs replaced by the class
above with a lower-case main. Case two: a copy of the project with
Program.cs removed and only Greeter.cs left. Both
dotnet build runs printed the CS5001 line above; the fixed case one passed
dotnet build --no-incremental -p:TreatWarningsAsErrors=true and ran.
Frequently asked
- Is Main case-sensitive in C#?
- Yes. The entry point must be named Main with a capital M and be static. A method called main is an ordinary method and the build fails with CS5001.
- I deleted Program.cs and now get CS5001. What do I do?
- Add a Program.cs back with at least one top-level statement, or a class with a static Main method. Since .NET 6 the template's entry point lives only in those top-level statements.
- How do I fix CS5001 in a class library?
- A library does not need Main. Make sure the project file does not say <OutputType>Exe</OutputType>; create library projects with dotnet new classlib so they build as a DLL.
A related layout error is CS8803, when a class sits above the top-level statements. More decoded errors are in the Fixes category.