87

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();
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
001
  • 62,807
  • 94
  • 230
  • 350

2 Answers2

192

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.

riQQ
  • 9,878
  • 7
  • 49
  • 66
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • added latest to myproject.csproj still getting the same error. – 001 Dec 01 '17 at 07:40
  • 4
    @001: See my edited answer - the LangVersion needs capital L and V, and you need to rename the method as well. – Jon Skeet Dec 01 '17 at 07:44
  • @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? – Jon Skeet Dec 21 '17 at 15:41
  • 6
    and 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 :-) – Pascal Dec 28 '17 at 11:07
  • What is the default LangVersion when left unspecified? – Steven Liekens Apr 29 '18 at 16:36
  • 1
    @StevenLiekens The latest major version - so 7.0 at the moment. – Jon Skeet Apr 29 '18 at 20:24
  • 11
    This was it for me. Language versions can also be set in the Properties page > Build Tab > Advanced – Stuart May 26 '18 at 10:58
  • 1
    @JonSkeet This works fine while building the code, but I get same error while trying to publish the app. Any workaround? – gaurav thakur Jul 11 '18 at 06:39
  • @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. – Jon Skeet Jul 11 '18 at 08:13
  • 2
    Thanks @JonSkeet for your reply :). It was silly mistake on side. I had only change Language version for Debug build and Publish uses Release build. – gaurav thakur Jul 18 '18 at 03:46
  • 1
    I lost about 30 minutes before I realized that the release configuration needed the same tweak that got my Debug version compiling. :-) – Wellspring Aug 07 '18 at 21:28
  • For 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. – ccalboni Oct 01 '18 at 09:46
  • how about for aspnetcore console application project. there is no where to define the version. – mokh223 Oct 05 '18 at 04:31
  • @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.) – Jon Skeet Oct 05 '18 at 12:11
  • Cannot believe after all these years this is still the ONLY solution. And BTW, is not even in VS2017 intellisense! – Fandango68 Jan 12 '20 at 06:37
  • 1
    @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. – Jon Skeet Jan 12 '20 at 08:22
  • Oddly 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. – Brian Sweeney Jul 11 '20 at 22:38
  • 1
    Changing Main's return type from void to Task is the piece I was missing. – Jacob Robbins Feb 12 '21 at 19:21
2

This may be a stupid answer, but if your intend was to make a Class Library project, make sure you have not created a Console Application project by mistake.

I know of at least one person who's done it.

typhon04
  • 2,350
  • 25
  • 22
  • I was helping a team member work on an MVC project and this was the problem. MVC projects need to have their `Output type` set to `Class Library`. I am guessing it had been changed to `Console Application` inadvertently. – Keith Banner Dec 07 '22 at 17:51