【问题标题】:Error Message "CS5001 Program does not contain a static 'Main' method suitable for an entry point", have multiple classes错误消息“CS5001 程序不包含适用于入口点的静态 'Main' 方法”,有多个类
【发布时间】:2018-06-25 07:03:54
【问题描述】:

所以这是交易,我目前有 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();

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

【问题讨论】:

  • 编译器告诉你出了什么问题:你没有Main 方法。如果您正在创建可执行文件,则需要一个静态的Main 方法,该方法在您运行可执行文件时调用。如果你想运行SituationSecondOne,你只需要把它变成一个静态的Main 方法。请注意,它确实需要是静态的。 (不清楚您所说的“我不能再将 public 添加到我想调用到第一类的函数”)

标签: c# compiler-errors


【解决方案1】:

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

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

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

   static void Main()
   {
   }

【讨论】:

  • 我将添加此评论来解释我的情况。也许其他人可能会因为和我一样的原因在这里绊倒。就我而言,我在项目解决方案中添加了一个空的新 Angular/AspNET Core 项目。删除了“Startup.cs”文件和其他不需要的文件。在构建解决方案时,我收到此错误,抱怨缺少“适合入口点的主要方法”。现在,@Ali Tooshmalani 建议将输出更改为“类库”而不是“控制台应用程序”,错误就消失了。谢谢你阿里;-)
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    可能是您的程序不包含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();
        }
    }
    

    【讨论】:

      【解决方案4】:

      请按照此方式操作并解决您的错误。

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

      【讨论】:

        【解决方案5】:

        在 VS2017 中运行我的代码时,这发生在我身上。 在 VS2019 中打开修复了错误。

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

        【讨论】:

          【解决方案6】:

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

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

          【讨论】:

            猜你喜欢
            • 2018-05-15
            • 1970-01-01
            • 1970-01-01
            • 2020-08-04
            • 1970-01-01
            • 1970-01-01
            • 2021-04-21
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多