错误信息“ CS5001程序不包含适用于入口点的静态‘ Main’方法”

无法执行下列代码 错误 CS5001程序不包含适合于入口点的静态‘ Main’方法

这个错误消息是什么意思?

class Program
{
static async Task MainAsync(string[] args)
{
Account.accountTest accountTest = new Account.accountTest();


bool result = await accountTest.CreateAccountAsync();
}
}
68954 次浏览

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.

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.