Error message "CS5001 Program does not contain a static 'Main' method suitable for an entry point"
64,032
It means that you don't have a suitable entry point for your application at the moment.
That code will nearly work with C# 7.1, but you do need to explicitly enable C# 7.1 in your project file:
<LangVersion>7.1</LangVersion>
or more generally:
<LangVersion>latest</LangVersion>
You also need to rename MainAsync
to Main
. So for example:
Program.cs:
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
await Task.Delay(1000);
}
}
ConsoleApp.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
</Project>
... builds and runs fine.
Related videos on Youtube
Author by
001
Only questions with complete answers are accepted as solutions.
Updated on July 08, 2022Comments
-
001 almost 2 years
Unable to execute the following code error CS5001 Program does not contain a static 'Main' method suitable for an entry point
What does this error message mean?
class Program { static async Task MainAsync(string[] args) { Account.accountTest accountTest = new Account.accountTest(); bool result = await accountTest.CreateAccountAsync(); } }
-
Eldritch Conundrum over 2 yearsI got the same error message because I had
async void Main
instead ofasync Task Main
-
-
001 over 6 yearsadded <langversion>latest</langversion> to myproject.csproj still getting the same error.
-
Jon Skeet over 6 years@001: See my edited answer - the LangVersion needs capital L and V, and you need to rename the method as well.
-
Jon Skeet over 6 years@Pascal: We need far more information than that to help you. Which exact version of the C# compiler (that's the important bit) are you using? What's the context?
-
Pascal over 6 yearsand that was the problem. After I found the Advanced button I saw its set to latest major version(default) which should mean 7.0. I set it directly to 7.2 c# compiler. Then it works :-)
-
Steven Liekens almost 6 yearsWhat is the default LangVersion when left unspecified?
-
Jon Skeet almost 6 years@StevenLiekens The latest major version - so 7.0 at the moment.
-
Stuart almost 6 yearsThis was it for me. Language versions can also be set in the Properties page > Build Tab > Advanced
-
gaurav thakur almost 6 years@JonSkeet This works fine while building the code, but I get same error while trying to publish the app. Any workaround?
-
Jon Skeet almost 6 years@gauravthakur: It's very hard to tell without any more information about what kind of project you're working with etc. I suggest you ask a new question with precise details.
-
gaurav thakur almost 6 yearsThanks @JonSkeet for your reply :). It was silly mistake on side. I had only change Language version for Debug build and Publish uses Release build.
-
Wellspring over 5 yearsI lost about 30 minutes before I realized that the release configuration needed the same tweak that got my Debug version compiling. :-)
-
ccalboni over 5 yearsFor those still with errors with language set correctly: make sure your Main signature is correct, with Task as a return type instead of void. If, like me, you only add "async" keyword the signature is not valid.
-
mokh223 over 5 yearshow about for aspnetcore console application project. there is no where to define the version.
-
Jon Skeet over 5 years@user2748728: I don't know exactly what you mean by "aspnetcore console application" but if you've got a project file, that's where you put the version. (And I haven't seen anything in .NET Core that doesn't have a project file.)
-
Fandango68 over 4 yearsCannot believe after all these years this is still the ONLY solution. And BTW, <LangVersion> is not even in VS2017 intellisense!
-
Jon Skeet over 4 years@Fandango68: With the latest versions of the .NET Core SDK (now that C# 8 has shipped) you don't need to specify the language version - at least not in VS2019.
-
Brian Sweeney almost 4 yearsOddly for me, choosing Latest Version did not work. I ended up setting it to the latest version by selecting it from the dropdown in the Advanced page. Makes no sense, but maybe worth a try if you're stuck.
-
Jacob Robbins about 3 yearsChanging Main's return type from void to Task is the piece I was missing.