【问题标题】:Error message "CS5001 Program does not contain a static 'Main' method suitable for an entry point"错误消息“CS5001 程序不包含适合入口点的静态 'Main' 方法”
【发布时间】:2018-05-15 06:31:56
【问题描述】:

无法执行以下代码 错误 CS5001 程序不包含适合入口点的静态“Main”方法

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

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

        bool result = await accountTest.CreateAccountAsync();
    }
}

【问题讨论】:

  • 我收到了同样的错误信息,因为我有 async void Main 而不是 async Task Main

标签: c# console visual-studio-2017 console-application .net-core-2.0


【解决方案1】:

这意味着您目前没有适合您的应用程序的入口点。

该代码几乎可以与 C# 7.1 一起使用,但您需要在项目文件中显式启用 C# 7.1:

<LangVersion>7.1</LangVersion>

或更笼统地说:

<LangVersion>latest</LangVersion>

您还需要将MainAsync 重命名为Main。比如:

程序.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>

...构建并运行良好。

【讨论】:

  • @001:查看我编辑的答案 - LangVersion 需要大写 L 和 V,您还需要重命名方法。
  • 这就是问题所在。在我找到高级按钮后,我看到它设置为最新的主要版本(默认),这应该意味着 7.0。我直接将它设置为 7.2 c# 编译器。然后它工作:-)
  • @StevenLiekens 最新的主要版本 - 目前是 7.0。
  • 这是给我的。语言版本也可以在属性页面 > 构建选项卡 > 高级中设置
  • 感谢@JonSkeet 的回复:)。这是一个愚蠢的错误。我只更改了 Debug 版本的语言版本,而 Publish 使用了 Release 版本。
猜你喜欢
  • 1970-01-01
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
  • 2020-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多