错误消息“CS5001 程序不包含适合入口点的静态‘Main’方法”,有多个类

问题描述 投票:0回答:8

所以,事情是这样的,我现在有 2 个类(计划添加多个),当我尝试从这两个类调用函数时,我收到此错误。相同的命名空间。我仔细检查并查看我的属性选项卡,发现它已设置为可编译。

using System;

namespace Game
{
public class SecondSet
{

    public void SituationSecondOne()
    {
        Console.WriteLine(" ");
        Console.WriteLine("Choices:");
        Console.WriteLine("1: First");
        Console.WriteLine("2: Second");
        Console.WriteLine(" ");

        int ChoiceOne = Convert.ToInt32(Console.ReadLine());

        switch (ChoiceOne)
        {
            case (1):
                Console.WriteLine("TEST2");
                break;
            case (2):
                Console.WriteLine("TEST2");
                break;
            case (1337):
                Console.WriteLine(" ");
                Console.WriteLine("Thank you for playing");
                Console.ReadLine();
                Environment.Exit(1);
                break;
            default:
                Console.WriteLine(" ");
                Console.WriteLine("Now, let's try that again ... (¬_¬)");
                SituationSecondOne();
                break;
        }
    }
 }
}

现在,当我从第二个到第一个调用该函数时,我没有收到任何错误。为此我需要什么类型的 Main() 方法? (我还尝试添加原来的 public void Main(string[] args),一旦添加,我就无法再将 public 添加到我要调用到第一个类的函数中)

注意:我将其添加到第一堂课中

SecondSet s2 = new SecondSet();

它工作正常,因为上面发布了代码,但我收到了提到的编译错误。请修复阀门 :/

c# compiler-errors
8个回答
7
投票

不清楚你的意思。但是 我也在研究这个问题,就我而言,解决方案太简单了。我向解决方案添加了一个新的空项目。新添加的项目会自动设置为控制台应用程序。但由于添加的项目是一个“空”项目,因此该新项目中不存在 Program.cs。 (如预期)

我需要做的就是将项目属性的输出类型更改为类库

将“项目”>“属性”下的“输出类型”更改为“类库”的输出类型。默认情况下,此设置可能已设置为“控制台应用程序”。

   static void Main()
   {
   }

5
投票

如果您将 Main 方法更改为异步方法,但忘记将 void 更改为 Task,您也可能会收到此错误。


1
投票

可能您的程序不包含 Main,它是控制台应用程序的入口点,因此替换为并阅读 this

 class Hello 
{

   public void SituationSecondOne()
{
    Console.WriteLine(" ");
    Console.WriteLine("Choices:");
    Console.WriteLine("1: First");
    Console.WriteLine("2: Second");
    Console.WriteLine(" ");

    int ChoiceOne = Convert.ToInt32(Console.ReadLine());

    switch (ChoiceOne)
    {
        case (1):
            Console.WriteLine("TEST2");
            break;
        case (2):
            Console.WriteLine("TEST2");
            break;
        case (1337):
            Console.WriteLine(" ");
            Console.WriteLine("Thank you for playing");
            Console.ReadLine();
            Environment.Exit(1);
            break;
        default:
            Console.WriteLine(" ");
            Console.WriteLine("Now, let's try that again ... (¬_¬)");
            SituationSecondOne();
            break;
    }
}
    static void Main() 
    {
        SecondSet s2 = new SecondSet();
        Console.ReadKey();
    }
}

1
投票

请按照此方式及其工作原理并解决您的错误。

public  class Program{
     static async Task Main()
             { }
 }

1
投票

对我来说,我设置了一个具有输出类型(控制台应用程序)的启动项目 无论如何,这是一个错误的项目,所以我将顶层设置为启动项目。

还将其他层更改为输出类型(类库)


1
投票

我在 VS2017 中运行代码时发生了这种情况。 在VS2019中打开修复了错误。

async Task Main() 入口点需要 C# 7.1 或更高版本,因此您可能没有安装所需的版本。


0
投票

由于OP不限于WinForms,并且由于混合WinForms => WPF方法而出现此错误,我觉得可以在这里回答。

就我而言,我有 .NET Framework 4.8,它从古老的 Outlook .pst 文件获取数据;除此之外,我想使用 WPF UI 显示联系人 - 我知道,我知道,为什么不使用 WinForms?

对于WPF,我必须添加System.Xaml,PresentationCore和PresentationFramework,当然删除了Form1.cs和Program.cs导致错误消息“CS5001程序不包含适合于立即出现的入口点”

经过一番谷歌搜索后,我来到这里并找到另一个线程,并在 .csproj 中提出了这个更改:

  <ItemGroup>
    <!--<Page Include="App.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </Page>-->
    <!-- ^^^^^^^^ ERROR ^^^^^^^^ -->
    <!-- Above leads to an error Message "CS5001 Program does not contain a static 'Main' method suitable for an entry point"-->
    
    <!-- Below is the fix for WPF in NET Framework --> 
    <ApplicationDefinition Include="App.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </ApplicationDefinition>
    ...
  <ItemGroup>

PS:顺便说一句,当您卸载项目并单击 Edit Project File 并要求使用 CTRL-F 查找 App.Xaml 时,您是否注意到 - 至少有时 - 默认范围是 Current Project?当您的项目被卸载时,尽管您位于 .csproj 中,但它在当前 .csproj 中找不到任何内容?

我在这方面浪费了很多时间:(在我意识到我必须将范围更改为当前文档...


0
投票

如果您在将控制台应用程序设置为使用顶级语句后遇到此问题,请确保您的program.cs 不为空,并且至少有一行可执行代码(例如 Console.WriteLine("Hello world")) .

© www.soinside.com 2019 - 2024. All rights reserved.