Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error message "CS5001 Program does not contain a static 'Main' method suitable for an entry point"

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();     } } 
like image 912
001 Avatar asked Dec 01 '17 07:12

001


People also ask

How do I fix cs5001 error?

If the Main method has an async modifier, make sure that the selected C# language version is 7.1 or higher and to use Task or Task<int> as the return type. The Main method is only required when compiling an executable file, that is, when the exe or winexe element of the TargetType compiler option is specified.

What is main method in C#?

The Main method is the entry point of a C# application. (Libraries and services do not require a Main method as an entry point.) When the application is started, the Main method is the first method that is invoked. There can only be one entry point in a C# program.


1 Answers

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.

like image 166
Jon Skeet Avatar answered Oct 25 '22 15:10

Jon Skeet